Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
describe('Connect createConnector component', () => {
const store = new Store();
store.addAction('reset-for-connect', () => {
let resetBuilder = updateBuilder()
.set('name', 'errorrik')
.set('emails', ['errorrik@gmail.com']);
return resetBuilder;
});
beforeEach(done => {
store.dispatch('reset-for-connect');
setTimeout(done, 1);
});
// 手动连接自己声明的store
const manualConnect = connect.createConnector(store);
it('data should be ready when component init', () => {
it('can data change by dispatch action', () => {
let store = new Store({
initData: {
name: 'errorrik',
emails: ['errorrik@gmail.com']
},
actions: {
changeName(name) {
return updateBuilder().set('name', name);
}
}
});
expect(store.getState().name).toBe('errorrik');
expect(store.getState().emails[0]).toBe('errorrik@gmail.com');
store.dispatch('changeName', 'erik');
expect(store.getState().name).toBe('erik');
it('remove listener by unlisten method', () => {
let store = new Store({
initData: {
name: 'errorrik',
emails: ['errorrik@gmail.com']
},
actions: {
change({name, email}) {
return updateBuilder().set('name', name).push('emails', email);
}
}
});
expect(store.getState().name).toBe('errorrik');
expect(store.getState().emails.length).toBe(1);
let fireTimes = 0;
it('children action not done, action shouldnot be mark done', done => {
let store = new Store({});
expect(store.getState().list == null).toBeTruthy();
store.addAction('fetchList', function (page, {getState, dispatch}) {
dispatch('showLoading');
dispatch('updateCurrentPage', page);
expect(getState('currentPage')).toBe(page);
return requestList(page).then(list => {
expect(getState('currentPage')).toBe(page);
if (getState('currentPage') === page) {
dispatch('sleep');
dispatch('hideLoading');
it('init data by constructor', () => {
let store = new Store({
initData: {
name: 'errorrik',
emails: ['errorrik@gmail.com']
}
});
expect(store.getState().name).toBe('errorrik');
expect(store.getState().emails[0]).toBe('errorrik@gmail.com');
expect(store.getState('name')).toBe('errorrik');
expect(store.getState('emails[0]')).toBe('errorrik@gmail.com');
});
it('listenable', () => {
let store = new Store({
initData: {
name: 'errorrik',
emails: ['errorrik@gmail.com']
},
actions: {
change({name, email}) {
return updateBuilder().set('name', name).push('emails', email);
}
}
});
expect(store.getState().name).toBe('errorrik');
expect(store.getState().emails.length).toBe(1);
let isChangeFired;
it('can add action after store created by "addAction"', () => {
let store = new Store({
initData: {
name: 'errorrik',
emails: ['errorrik@gmail.com']
}
});
expect(store.getState().name).toBe('errorrik');
expect(store.getState().emails[0]).toBe('errorrik@gmail.com');
store.addAction('changeName', name => updateBuilder().set('name', name));
store.dispatch('changeName', 'erik');
expect(store.getState().name).toBe('erik');
});
it('update depend on current state', () => {
let store = new Store({
initData: {
name: 'errorrik',
emails: ['errorrik@gmail.com']
}
});
expect(store.getState().name).toBe('errorrik');
expect(store.getState().emails[0]).toBe('errorrik@gmail.com');
store.addAction('change', function (info, {getState}) {
let builder = updateBuilder().set('name', info.name);
if (!getState('emails[0]')) {
builder = builder.push('emails', info.email);
}
it('dispatch async action, promise resolve value is unuseful', done => {
let store = new Store({
initData: {
name: 'errorrik',
emails: ['errorrik@gmail.com']
}
});
expect(store.getState().name).toBe('errorrik');
expect(store.getState().emails[0]).toBe('errorrik@gmail.com');
store.addAction('changeName', function (name, {dispatch}) {
return new Promise(function (resolve) {
setTimeout(() => {
dispatch('setName', name);
resolve(updateBuilder().set('name', 'hello'));
}, 200);
});
it('dispatch async action in action', done => {
let store = new Store({});
expect(store.getState().list == null).toBeTruthy();
store.addAction('fetchList', function (payload, {dispatch, getState}) {
dispatch('loadingState', true);
return requestList().then(list => {
dispatch('loadingState', false);
expect(getState('loading')).not.toBeTruthy();
expect(getActionInfo().done).not.toBeTruthy();
dispatch('updateList', list);
});
});
store.addAction('updateList', function (list) {