UNPKG

3.89 kBJavaScriptView Raw
1import test from 'tape'
2import Alt from 'alt';
3import ActionListeners from 'alt-utils/lib/ActionListeners';
4
5import {createStore, callFactory, getSources, setAltInstance} from '../src';
6
7
8function resetAlt() {
9 const alt = new Alt();
10 setAltInstance(alt);
11 return alt;
12}
13
14
15test('createStore: uses alt instance specified globally', (t) => {
16 const alt = new Alt();
17 setAltInstance(alt);
18 const store = createStore('MyStore');
19 t.equals(store.alt, alt);
20 t.end();
21});
22test('createStore: uses specified alt instance', (t) => {
23 const alt = new Alt();
24 const store = createStore('MyStore', {alt: alt});
25 t.equals(store.alt, alt);
26 t.end();
27});
28test('createStore: throws when displayName is missing', (t) => {
29 resetAlt();
30 t.throws(() => createStore(), /displayName is required/);
31 t.end();
32});
33test('createStore: does not throw if displayName is specified', (t) => {
34 resetAlt();
35 t.doesNotThrow(() => createStore('MyStore'), /displayName is required/);
36 t.end();
37});
38test('createStore: uses specified displayName', (t) => {
39 resetAlt();
40 const store = createStore('MyStore');
41 t.equals(store.displayName, 'MyStore');
42 t.end();
43});
44
45
46
47
48test('createStore: bound calls are exposed as store methods', (t) => {
49 resetAlt();
50 const call = callFactory('myCall').create({
51 dataSource: {
52 remote: () => Promise.resolve()
53 }
54 });
55 const calls = [call];
56 const store = createStore('MyStore', {calls});
57 t.equals(typeof store.myCall, 'function');
58 t.end();
59});
60
61
62
63test('createStore: bound calls dispatch lifecycle actions', async (t) => {
64
65 const alt = resetAlt();
66 const goodCall = callFactory('goodCall').create({
67 dataSource: {
68 remote: () => Promise.resolve('ok')
69 }
70 });
71 const badCall = callFactory('badCall').create({
72 dataSource: {
73 remote: () => Promise.reject(new Error('nok'))
74 }
75 });
76 const store = createStore('MyStore', {
77 calls: [
78 goodCall,
79 badCall
80 ]
81 });
82
83 const dispatched = {
84 goodCall: {
85 loading: false,
86 error: false,
87 success: false
88 },
89 badCall: {
90 loading: false,
91 error: false,
92 success: false
93 }
94 };
95
96
97 const listeners = new ActionListeners(alt);
98
99 listeners.addActionListener(goodCall.actions.LOADING, () => dispatched.goodCall.loading = true);
100 listeners.addActionListener(goodCall.actions.ERROR, () => dispatched.goodCall.error = true);
101 listeners.addActionListener(goodCall.actions.SUCCESS, () => dispatched.goodCall.success = true);
102
103 listeners.addActionListener(badCall.actions.LOADING, () => dispatched.badCall.loading = true);
104 listeners.addActionListener(badCall.actions.ERROR, () => dispatched.badCall.error = true);
105 listeners.addActionListener(badCall.actions.SUCCESS, () => dispatched.badCall.success = true);
106
107 await store.goodCall();
108
109 try {
110 await store.badCall();
111 }
112 catch (error) {
113 // do nothing here.
114 // this is support to throw - we want to assert that the error action was dispatched.
115 }
116
117
118 t.equals(dispatched.goodCall.loading, true, 'lifecycle: loading action dispatched');
119 t.equals(dispatched.goodCall.error, false, 'lifecycle: error action not dispatched');
120 t.equals(dispatched.goodCall.success, true, 'lifecycle: success action dispatched');
121
122 t.equals(dispatched.badCall.loading, true, 'lifecycle: loading action dispatched');
123 t.equals(dispatched.badCall.error, true, 'lifecycle: error action not dispatched');
124 t.equals(dispatched.badCall.success, false, 'lifecycle: success action not dispatched');
125
126
127 t.end();
128});
129