Skip to content

Commit f8e6e0b

Browse files
committed
deleted useless files and increased test coverage
1 parent b448744 commit f8e6e0b

4 files changed

Lines changed: 91 additions & 5 deletions

File tree

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"tasks": {
3-
"test": "rm -r .cov_profile && rm .cov_profile.lcov && rm -r cov && deno test --coverage=.cov_profile && deno coverage .cov_profile --lcov --output=.cov_profile.lcov && genhtml -o cov/html .cov_profile.lcov && open cov/html/index.html"
3+
"test": "rm -rf .cov_profile && rm -f .cov_profile.lcov && rm -rf cov && deno test --coverage=.cov_profile && deno coverage .cov_profile --lcov --output=.cov_profile.lcov && genhtml -o cov/html .cov_profile.lcov && open cov/html/index.html"
44
}
55
}

deps.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

dev_deps.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/mod.test.ts

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Deno.test("A store can be added to a stack without providing a pointer, accessed
174174
assertEquals(stack.get<number>(ptr)!.state, store.state, "The states should match");
175175

176176
// Cannot be added twice at address without override
177-
assertThrows(() => stack.addStoreAtPointer(store, ptr), mod.MemoryAllocationError, "Attempted to insert store at already allocated memory address without explicit override.", "Should not be able to add at address without override.");
177+
assertThrows(() => stack.addStoreAtPointer(store, ptr, { verbose: true }), mod.MemoryAllocationError, "Attempted to insert store at already allocated memory address without explicit override.", "Should not be able to add at address without override.");
178178

179179
// Can be overriden
180180
stack.addStoreAtPointer(store2, ptr, { override: true });
@@ -187,7 +187,7 @@ Deno.test("A store can be added to a stack without providing a pointer, accessed
187187
assertEquals(typeof stack.get(ptr), "undefined", "The pointer should be unallocated");
188188

189189
// But cannot be removed twice
190-
assertThrows(() => stack.removeStore(ptr), mod.NullPointerError, "Attempted to access unallocated memory address.", "Should not be able to delete the store twice.");
190+
assertThrows(() => stack.removeStore(ptr, { verbose: true }), mod.NullPointerError, "Attempted to access unallocated memory address.", "Should not be able to delete the store twice.");
191191
});
192192

193193
Deno.test("upsert test", () => {
@@ -228,4 +228,92 @@ Deno.test("upsert test", () => {
228228

229229
// Throwing on reattaching observer
230230
assertThrows(() => stack.upsert(defaultValue1, pointer1, observer), mod.DuplicateObserverError, "This observer is already attached to the store.", "Should trigger a duplicate observer error");
231+
});
232+
233+
// Mark: useStore tests
234+
Deno.test("useStore without options", () => {
235+
const stateValue = "Test";
236+
const pointer = mod.useStore(stateValue);
237+
238+
assertExists(mod.Stores.get(pointer), "Store should exist at returned address.");
239+
assertEquals(mod.Stores.get<string>(pointer)!.state, stateValue, "The store should contain the initial provided value");
240+
});
241+
242+
Deno.test("useStore with callback", () => {
243+
const startValue = 0;
244+
const finalValue = 1;
245+
246+
let observerOutput = startValue;
247+
const observerCb = (state: number) => observerOutput = state;
248+
249+
const pointer = mod.useStore(startValue, { onChange: observerCb });
250+
251+
mod.Stores.get<number>(pointer)!.set(finalValue);
252+
253+
assertEquals(observerOutput, finalValue, "observerOutput should match finalValue");
254+
});
255+
256+
Deno.test("useStore with pointer", () => {
257+
const pointer = crypto.randomUUID();
258+
const value = 0;
259+
260+
assertEquals(mod.useStore(value, { pointer: pointer }), pointer, "Returned pointer should match provided pointer");
261+
assertEquals(mod.useStore(value, { pointer: pointer }), pointer, "Should not trigger an error blocking false");
262+
263+
// with override
264+
const value2 = 1;
265+
266+
mod.useStore(value2, { pointer: pointer, override: true });
267+
268+
assertEquals(mod.Stores.get<number>(pointer)!.state, value2, "Should match value2");
269+
270+
// Add cb
271+
const finalValue = 2;
272+
let outputValue = mod.Stores.get<number>(pointer)!.state;
273+
const callback = (state: number) => outputValue = state;
274+
275+
mod.useStore(finalValue, { pointer: pointer, onChange: callback });
276+
277+
assertEquals(outputValue, value2, "Should equal value 2 before any changes");
278+
279+
mod.Stores.get<number>(pointer)!.set(finalValue);
280+
281+
assertEquals(outputValue, finalValue, "Should equal final value.");
282+
283+
// Testing duplicate observers
284+
class ConcreteObserver implements mod.Observer<number> {
285+
public update(_subject: mod.Store<number>): void {
286+
return;
287+
}
288+
}
289+
290+
const observer = new ConcreteObserver();
291+
292+
assertThrows(
293+
() => {
294+
mod.useStore(finalValue, {
295+
pointer: pointer,
296+
override: true,
297+
errorHandling: { verbose: true, stopOnError: true },
298+
observers: [observer, observer] // Duplicate observers
299+
});
300+
},
301+
mod.DuplicateObserverError
302+
);
303+
304+
assertThrows(
305+
() => {
306+
mod.useStore(finalValue, {
307+
pointer: pointer,
308+
override: false,
309+
errorHandling: { verbose: true, stopOnError: true },
310+
observers: [observer, observer] // Duplicate observers
311+
});
312+
},
313+
mod.DuplicateObserverError
314+
);
315+
});
316+
317+
Deno.test("misc tests", () => {
318+
mod.StoreStack.configure();
231319
});

0 commit comments

Comments
 (0)