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', () => {
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', () => {
let MyComponent = manualConnect({
name: 'name',
email: 'emails[0]'
})(san.defineComponent({
template: '<u title="{{name}}-{{email}}">{{name}}-{{email}}</u>'
}));
let myComponent = new MyComponent();
let wrap = document.createElement('div');
document.body.appendChild(wrap);
myComponent.attach(wrap);
let u = wrap.getElementsByTagName('u')[0];
expect(u.title).toBe('errorrik-errorrik@gmail.com');
it('dispatch action method should connect to component "actions" member, object mapActions', done => {
store.addAction('for-connect-3', payload => {
let builder = updateBuilder()
.set('name', payload.name)
.set('emails[0]', payload.email);
return builder;
});
let MyComponent = connect.san(
{
name: 'name',
email: 'emails[0]'
},
{
updateInfo: 'for-connect-3'
}
)(
it('async action should return Promise', done => {
store.addAction('for-connect-7', (name, {dispatch}) => {
return new Promise(function (resolve) {
setTimeout(() => {
dispatch('for-connect-8', name);
resolve();
}, 100);
});
});
store.addAction('for-connect-8', name => {
return updateBuilder().set('forConnect7', name);
});
let MyComponent = connect.san(
{name: 'forConnect7'},
{change: 'for-connect-7'}
)(
it('component data should be update when store data change, function mapStates item', done => {
store.addAction('for-connect-2', payload => {
let builder = updateBuilder()
.set('name', payload.name)
.set('emails[0]', payload.email);
return builder;
});
let MyComponent = connect.san({
name: 'name',
email: state => {
return state.emails[0];
}
})(
san.defineComponent({
template: '<u title="{{name}}-{{email}}">{{name}}-{{email}}</u>'
})
);
let myComponent = new MyComponent();
let wrap = document.createElement('div');
document.body.appendChild(wrap);
myComponent.attach(wrap);
let u = wrap.getElementsByTagName('u')[0];
it('components data should not infulence each other, and push item', done => {
store.addAction('for-connect-5', email => {
let builder = updateBuilder().push('persons[0].emails', email);
return builder;
});
let MyComponent = connect.san({
name: 'persons[0].name',
emails: 'persons[0].emails'
})(
san.defineComponent({
template: '<dl><dt title="{{name}}">{{name}}</dt><dd title="{{email}}">{{email}}</dd></dl>'
})
);
store.dispatch('for-connect-persons', [
{name: 'erik', emails: ['erik168@163.com', 'errorrik@gmail.com']}
]);
let wrap = document.createElement('div');
document.body.appendChild(wrap);
let myComponent1 = new MyComponent();
myComponent1.attach(wrap);
let myComponent2 = new MyComponent();
myComponent2.attach(wrap);
let dts = wrap.getElementsByTagName('dt');
let dds = wrap.getElementsByTagName('dd');
expect(dts.length).toBe(2);
expect(dds.length).toBe(4);
expect(dts[0].title).toBe('erik');
it('set errors and clear errors', () => {
store.dispatch(Types.ERRORS_SET, {title: 'one'});
let errors = store.getState('errors');
expect(errors.length).toBe(1);
expect(errors[0]).toBe('title one');
store.dispatch(Types.ERRORS_CLEAR);
expect(store.getState('errors') == null).toBeTruthy();
});
});
it('set errors and clear errors', () => {
store.dispatch(Types.ERRORS_SET, {title: 'one'});
let errors = store.getState('errors');
expect(errors.length).toBe(1);
expect(errors[0]).toBe('title one');
store.dispatch(Types.ERRORS_CLEAR);
expect(store.getState('errors') == null).toBeTruthy();
});
});
it('fetch list', done => {
let fetchPromise = store.dispatch(Types.FETCH);
expect(store.getState('articlesLoading')).toBeTruthy();
fetchPromise.then(() => {
expect(store.getState('articlesLoading')).toBeFalsy();
expect(store.getState('articleCount')).toBe(500);
expect(store.getState('articlePageCount') >= store.getState('articleCount') / Conf.PAGE_SIZE).toBeTruthy();
expect(store.getState('articles') instanceof Array).toBeTruthy();
done();
});
});
});
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');