2020-12-03 13:07:58 +08:00
|
|
|
import {getSnapshot, getEnv, onSnapshot} from 'mobx-state-tree';
|
2022-06-02 10:00:09 +08:00
|
|
|
import {StoreNode} from '../../src';
|
|
|
|
import {ServiceStore} from '../../src';
|
|
|
|
import {RendererStore} from '../../src';
|
2019-04-30 11:11:25 +08:00
|
|
|
import omit = require('lodash/omit');
|
|
|
|
|
|
|
|
test('store:ServiceStore', () => {
|
2020-12-03 13:07:58 +08:00
|
|
|
const store = ServiceStore.create({
|
|
|
|
id: '1',
|
|
|
|
storeType: ServiceStore.name
|
|
|
|
});
|
2019-04-30 11:11:25 +08:00
|
|
|
|
2020-12-03 13:07:58 +08:00
|
|
|
expect(getSnapshot(store)).toMatchSnapshot();
|
2019-04-30 11:11:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('store:ServiceStore fetchInitData success', async () => {
|
2020-12-03 13:07:58 +08:00
|
|
|
const fetcher = jest.fn().mockImplementationOnce(() =>
|
|
|
|
Promise.resolve({
|
|
|
|
ok: true,
|
|
|
|
data: {
|
|
|
|
a: 1,
|
|
|
|
b: 2
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
const isCancel = jest.fn(() => false);
|
|
|
|
const mainStore = RendererStore.create(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
fetcher,
|
|
|
|
isCancel
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const states: Array<any> = [];
|
|
|
|
|
|
|
|
const store = ServiceStore.create(
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
storeType: ServiceStore.name
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fetcher,
|
|
|
|
isCancel
|
|
|
|
}
|
|
|
|
);
|
|
|
|
mainStore.addStore(store);
|
|
|
|
|
|
|
|
onSnapshot(store, snapshot => states.push(snapshot));
|
|
|
|
|
|
|
|
await store.fetchInitData('/api/xxx');
|
|
|
|
|
|
|
|
const ignoreUdatedAt = states.map(snapshot => omit(snapshot, ['updatedAt']));
|
|
|
|
expect(ignoreUdatedAt).toMatchSnapshot();
|
|
|
|
|
|
|
|
expect(states.length).toBe(2);
|
|
|
|
expect(states[1].updatedAt).not.toEqual(states[0].updatedAt);
|
2019-04-30 11:11:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('store:ServiceStore fetchInitData failed', async () => {
|
2020-12-03 13:07:58 +08:00
|
|
|
const fetcher = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementationOnce(() => Promise.reject('Network Error'));
|
|
|
|
const notify = jest.fn();
|
|
|
|
const isCancel = jest.fn(() => false);
|
|
|
|
const mainStore = RendererStore.create(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
fetcher,
|
|
|
|
notify,
|
|
|
|
isCancel
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const states: Array<any> = [];
|
|
|
|
|
|
|
|
const store = ServiceStore.create(
|
|
|
|
{
|
|
|
|
id: '1',
|
|
|
|
storeType: ServiceStore.name
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fetcher,
|
|
|
|
notify,
|
|
|
|
isCancel
|
|
|
|
}
|
|
|
|
);
|
|
|
|
mainStore.addStore(store);
|
|
|
|
|
|
|
|
onSnapshot(store, snapshot => states.push(snapshot));
|
|
|
|
|
|
|
|
await store.fetchInitData('/api/xxx');
|
|
|
|
expect(states).toMatchSnapshot();
|
|
|
|
expect(notify).toHaveBeenCalled();
|
|
|
|
expect(notify).toHaveBeenLastCalledWith('error', 'Network Error');
|
|
|
|
expect(isCancel).toHaveBeenCalled();
|
|
|
|
});
|