added some more tests

This commit is contained in:
Chris Kruining 2025-02-20 11:36:35 +11:00
parent 20a0fc679b
commit 04b55e02fb
No known key found for this signature in database
GPG key ID: EB894A3560CCCAD2
5 changed files with 168 additions and 47 deletions

View file

@ -54,6 +54,7 @@ describe('dataset', () => {
});
it('update if the source value changes', () => {
// Arrange
const [data, setData] = createSignal([
{ id: '1', name: 'a first name', amount: 30 },
{ id: '2', name: 'a second name', amount: 20 },
@ -61,28 +62,25 @@ describe('dataset', () => {
]);
const dataset = createDataSet(data);
dataset.mutateEach(item => ({ ...item, amount: item.amount * 2 }))
dataset.mutateEach(item => ({ ...item, amount: item.amount * 2 }));
// Act
setData([
{ id: '4', name: 'a first name', amount: 30 },
{ id: '5', name: 'a second name', amount: 20 },
{ id: '6', name: 'a third name', amount: 10 },
]);
// Assert
return testEffect(done =>
createEffect((run: number = 0) => {
data();
createEffect(() => {
expect(dataset.value).toEqual([
{ id: '4', name: 'a first name', amount: 60 },
{ id: '5', name: 'a second name', amount: 40 },
{ id: '6', name: 'a third name', amount: 20 },
])
if (run === 0) {
setData([
{ id: '4', name: 'a first name', amount: 30 },
{ id: '5', name: 'a second name', amount: 20 },
{ id: '6', name: 'a third name', amount: 10 },
]);
} else if (run === 1) {
expect(dataset.value).toEqual([
{ id: '4', name: 'a first name', amount: 60 },
{ id: '5', name: 'a second name', amount: 40 },
{ id: '6', name: 'a third name', amount: 20 },
])
done()
}
return run + 1
done()
})
);
});

View file

@ -0,0 +1,49 @@
import { describe, expect } from "vitest";
import { createSource } from "./source";
import { it } from "~/test-helpers";
import { testEffect } from "@solidjs/testing-library";
import { createEffect, createSignal } from "solid-js";
describe('Source', () => {
describe('Source', () => {
it('should return a `Source`', () => {
// Arrange
// Act
const actual = createSource('');
// Assert
expect(actual.out).toBe('');
});
it('should transform the input format to output format', () => {
// Arrange
const given = '**text**\n';
const expected = '<p><strong>text</strong></p>';
// Act
const actual = createSource(given);
// Assert
expect(actual.out).toBe(expected);
});
it('should contain query results', () => {
// Arrange
const expected: [number, number][] = [[8, 9], [12, 13], [15, 16]];
const source = createSource('this is a seachable string');
// Act
source.query = 'a';
// Assert
return testEffect(done => {
createEffect(() => {
expect(source.queryResults).toEqual(expected);
done()
});
});
});
});
});

View file

@ -37,16 +37,12 @@ const inToOutProcessor = unified().use(remarkParse).use(remarkRehype).use(rehype
const outToInProcessor = unified().use(rehypeParse).use(rehypeRemark).use(remarkStringify, { bullet: '-' });
export function createSource(initalValue: string): Source {
const [store, setStore] = createStore<SourceStore>({ in: initalValue, out: '', plain: '', query: '', metadata: { spellingErrors: [], grammarErrors: [], queryResults: [] } });
onMount(() => {
const ast = inToOutProcessor.runSync(inToOutProcessor.parse(initalValue));
const ast = inToOutProcessor.runSync(inToOutProcessor.parse(initalValue));
const out = String(inToOutProcessor.stringify(ast));
const plain = String(unified().use(plainTextStringify).stringify(ast));
setStore({
out: String(inToOutProcessor.stringify(ast)),
plain: String(unified().use(plainTextStringify).stringify(ast)),
});
});
const [store, setStore] = createStore<SourceStore>({ in: initalValue, out, plain, query: '', metadata: { spellingErrors: [], grammarErrors: [], queryResults: [] } });
createEffect(() => {
const value = store.plain;