2
0
mirror of https://github.com/frappe/books.git synced 2024-11-09 23:30:56 +00:00
books/fyo/tests/testObservable.spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-10-31 07:33:50 +00:00
import { strictEqual } from 'assert';
import { assertThrows } from 'backend/database/tests/helpers';
2022-04-27 18:29:57 +00:00
import Observable from 'fyo/utils/observable';
2022-10-31 07:33:50 +00:00
import test from 'tape';
2022-04-27 18:29:57 +00:00
enum ObsEvent {
A = 'event-a',
B = 'event-b',
}
2022-10-31 07:33:50 +00:00
const obs = new Observable();
let counter = 0;
const params = { aOne: 18, aTwo: 21, b: 42 };
2022-04-27 18:29:57 +00:00
2022-10-31 07:33:50 +00:00
const listenerAOnce = (value: number) => {
strictEqual(params.aOne, value, 'listenerAOnce');
};
2022-04-27 18:29:57 +00:00
2022-10-31 07:33:50 +00:00
const listenerAEvery = (value: number) => {
if (counter === 0) {
strictEqual(params.aOne, value, 'listenerAEvery 0');
} else if (counter === 1) {
strictEqual(params.aTwo, value, 'listenerAEvery 1');
} else {
throw new Error("this shouldn't run");
}
counter += 1;
};
2022-04-27 18:29:57 +00:00
2022-10-31 07:33:50 +00:00
const listenerBOnce = (value: number) => {
strictEqual(params.b, value, 'listenerBOnce');
};
2022-04-27 18:29:57 +00:00
test('set A One', (t) => {
2022-10-31 07:33:50 +00:00
t.equal(obs.hasListener(ObsEvent.A), false, 'pre');
2022-04-27 18:29:57 +00:00
2022-10-31 07:33:50 +00:00
obs.once(ObsEvent.A, listenerAOnce);
t.equal(obs.hasListener(ObsEvent.A), true, 'non specific');
t.equal(obs.hasListener(ObsEvent.A, listenerAOnce), true, 'specific once');
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), false, 'specific every');
t.end();
2022-10-31 07:33:50 +00:00
});
2022-04-27 18:29:57 +00:00
test('set A Two', (t) => {
2022-10-31 07:33:50 +00:00
obs.on(ObsEvent.A, listenerAEvery);
t.equal(obs.hasListener(ObsEvent.A), true, 'non specific');
t.equal(obs.hasListener(ObsEvent.A, listenerAOnce), true, 'specific once');
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), true, 'specific every');
t.end();
2022-10-31 07:33:50 +00:00
});
2022-04-27 18:29:57 +00:00
test('set B', (t) => {
2022-10-31 07:33:50 +00:00
t.equal(obs.hasListener(ObsEvent.B), false, 'pre');
2022-04-27 18:29:57 +00:00
2022-10-31 07:33:50 +00:00
obs.once(ObsEvent.B, listenerBOnce);
t.equal(obs.hasListener(ObsEvent.A, listenerBOnce), false, 'specific false');
t.equal(obs.hasListener(ObsEvent.B, listenerBOnce), true, 'specific true');
t.end();
2022-10-31 07:33:50 +00:00
});
2022-04-27 18:29:57 +00:00
test('trigger A 0', async (t) => {
2022-10-31 07:33:50 +00:00
await obs.trigger(ObsEvent.A, params.aOne);
t.equal(obs.hasListener(ObsEvent.A), true, 'non specific');
t.equal(obs.hasListener(ObsEvent.A, listenerAOnce), false, 'specific');
});
2022-04-27 18:29:57 +00:00
test('trigger A 1', async (t) => {
2022-10-31 07:33:50 +00:00
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), true, 'specific pre');
await obs.trigger(ObsEvent.A, params.aTwo);
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), true, 'specific post');
});
2022-04-27 18:29:57 +00:00
test('trigger B', async (t) => {
2022-10-31 07:33:50 +00:00
t.equal(obs.hasListener(ObsEvent.B, listenerBOnce), true, 'specific pre');
await obs.trigger(ObsEvent.B, params.b);
t.equal(obs.hasListener(ObsEvent.B, listenerBOnce), false, 'specific post');
});
2022-04-27 18:29:57 +00:00
test('remove A', async (t) => {
2022-10-31 07:33:50 +00:00
obs.off(ObsEvent.A, listenerAEvery);
t.equal(obs.hasListener(ObsEvent.A, listenerAEvery), false, 'specific pre');
2022-04-27 18:29:57 +00:00
2022-10-31 07:33:50 +00:00
t.equal(counter, 2, 'incorrect counter');
await obs.trigger(ObsEvent.A, 777);
2022-04-27 18:29:57 +00:00
});
test('observable trigger error propagation', async (t) => {
const obs = new Observable();
obs.on('testOne', () => {
throw new Error('stuff');
});
await assertThrows(async () => {
await obs.trigger('testOne');
t.ok(false, 'trigger should throw error');
});
t.ok(true, 'assert throws success');
});