88 lines
2.5 KiB
TypeScript
88 lines
2.5 KiB
TypeScript
|
|
import { test } from 'node:test';
|
||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import { createStore } from 'jotai';
|
||
|
|
import { toastQueueAtom, dismissToastAtom, ToastNotif } from './toast';
|
||
|
|
|
||
|
|
// The queue lives in an unexported baseAtom; we drive the two write-only setters
|
||
|
|
// (toastQueueAtom append + null no-op guard, dismissToastAtom remove-by-id)
|
||
|
|
// through a jotai store and read back via toastQueueAtom's getter.
|
||
|
|
|
||
|
|
const makeToast = (id: string): ToastNotif => ({
|
||
|
|
id,
|
||
|
|
displayName: `name-${id}`,
|
||
|
|
body: `body-${id}`,
|
||
|
|
roomName: `room-${id}`,
|
||
|
|
roomId: `!${id}:server`,
|
||
|
|
});
|
||
|
|
|
||
|
|
test('starts empty', () => {
|
||
|
|
const store = createStore();
|
||
|
|
assert.deepEqual(store.get(toastQueueAtom), []);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('toastQueueAtom appends in order', () => {
|
||
|
|
const store = createStore();
|
||
|
|
const a = makeToast('a');
|
||
|
|
const b = makeToast('b');
|
||
|
|
store.set(toastQueueAtom, a);
|
||
|
|
store.set(toastQueueAtom, b);
|
||
|
|
assert.deepEqual(
|
||
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
||
|
|
['a', 'b'],
|
||
|
|
);
|
||
|
|
assert.equal(store.get(toastQueueAtom)[0], a);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('toastQueueAtom ignores null (no-op guard)', () => {
|
||
|
|
const store = createStore();
|
||
|
|
store.set(toastQueueAtom, makeToast('a'));
|
||
|
|
store.set(toastQueueAtom, null);
|
||
|
|
assert.deepEqual(
|
||
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
||
|
|
['a'],
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('toastQueueAtom allows duplicate ids (no dedupe)', () => {
|
||
|
|
const store = createStore();
|
||
|
|
store.set(toastQueueAtom, makeToast('a'));
|
||
|
|
store.set(toastQueueAtom, makeToast('a'));
|
||
|
|
assert.equal(store.get(toastQueueAtom).length, 2);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('dismissToastAtom removes the matching id only', () => {
|
||
|
|
const store = createStore();
|
||
|
|
store.set(toastQueueAtom, makeToast('a'));
|
||
|
|
store.set(toastQueueAtom, makeToast('b'));
|
||
|
|
store.set(toastQueueAtom, makeToast('c'));
|
||
|
|
|
||
|
|
store.set(dismissToastAtom, 'b');
|
||
|
|
assert.deepEqual(
|
||
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
||
|
|
['a', 'c'],
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('dismissToastAtom removes every entry sharing the id', () => {
|
||
|
|
const store = createStore();
|
||
|
|
store.set(toastQueueAtom, makeToast('a'));
|
||
|
|
store.set(toastQueueAtom, makeToast('a'));
|
||
|
|
store.set(toastQueueAtom, makeToast('b'));
|
||
|
|
|
||
|
|
store.set(dismissToastAtom, 'a');
|
||
|
|
assert.deepEqual(
|
||
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
||
|
|
['b'],
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('dismissToastAtom for an unknown id is a no-op', () => {
|
||
|
|
const store = createStore();
|
||
|
|
store.set(toastQueueAtom, makeToast('a'));
|
||
|
|
store.set(dismissToastAtom, 'missing');
|
||
|
|
assert.deepEqual(
|
||
|
|
store.get(toastQueueAtom).map((t) => t.id),
|
||
|
|
['a'],
|
||
|
|
);
|
||
|
|
});
|