UNPKG

2.75 kBJavaScriptView Raw
1import test from 'tape';
2import Alt from 'alt';
3import ActionListeners from 'alt-utils/lib/ActionListeners';
4
5import {callFactory, createStore} from '../src';
6import {setAltInstance} from '../src/altInstance';
7
8function resetAlt() {
9 const alt = new Alt();
10 setAltInstance(alt);
11 return alt;
12}
13
14
15test('callFactory throws without a given name', (t) => {
16 resetAlt();
17 t.throws(() => callFactory());
18 t.end();
19});
20test('callFactory does not throw with a given name', (t) => {
21 resetAlt();
22 t.doesNotThrow(() => callFactory('myCall'));
23 t.end();
24});
25
26test('callFactory: create() throws without a given object', (t) => {
27 resetAlt();
28 const call = callFactory('myCall');
29 t.throws(() => call.create());
30 t.end();
31});
32
33test('callFactory: create() throws without a proper dataSource', (t) => {
34 resetAlt();
35 const call = callFactory('myCall');
36 t.throws(() => call.create({}));
37 t.throws(() => call.create({dataSource: {}}));
38 t.end();
39});
40
41test('callFactory: create() does not throw when given a proper dataSource ', (t) => {
42 resetAlt();
43 const call = callFactory('myCall');
44 t.doesNotThrow(() => call.create({
45 dataSource: {
46 remote: () => Promise.resolve()
47 }
48 }));
49 t.end();
50});
51
52test('callFactory: uses custom named actions', async (t) => {
53 const alt = resetAlt();
54
55 let remote = () => Promise.resolve();
56
57 const call = callFactory('myCall', {defaultActions: ['foo', 'bar', 'baz']}).create(({actions}) => {
58 return {
59 dataSource: {
60 loading: actions.foo,
61 error: actions.bar,
62 success: actions.baz,
63 remote: () => remote()
64 }
65 };
66 });
67
68 const store = createStore('MyStore', {calls: [call]});
69
70 const dispatched = {
71 loading: false,
72 success: false,
73 error: false
74 };
75
76 const listeners = new ActionListeners(alt);
77 listeners.addActionListener(call.actions.FOO, () => dispatched.loading = true);
78 listeners.addActionListener(call.actions.BAR, () => dispatched.error = true);
79 listeners.addActionListener(call.actions.BAZ, () => dispatched.success = true);
80
81 await store.myCall();
82 t.equals(dispatched.loading, true, 'custom loading action dispatched');
83 t.equals(dispatched.success, true, 'custom success action dispatched');
84
85 remote = () => Promise.reject(new Error('nok'));
86 try {
87 await store.myCall();
88 }
89 catch (error) {
90 // do nothing
91 // expected error.
92 }
93
94 t.equals(dispatched.error, true, 'custom error action dispatched');
95 t.end();
96});