2016-10-30 17:42:49 +05:30
|
|
|
import expect from 'unexpected';
|
|
|
|
|
|
|
|
import accounts from 'components/accounts/reducer';
|
2016-11-05 15:41:41 +05:30
|
|
|
import {
|
|
|
|
updateToken, add, remove, activate,
|
|
|
|
ADD, REMOVE, ACTIVATE, UPDATE_TOKEN
|
|
|
|
} from 'components/accounts/actions';
|
2016-10-30 17:42:49 +05:30
|
|
|
|
|
|
|
const account = {
|
|
|
|
id: 1,
|
|
|
|
username: 'username',
|
|
|
|
email: 'email@test.com',
|
|
|
|
token: 'foo',
|
|
|
|
refreshToken: 'foo'
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('Accounts reducer', () => {
|
|
|
|
let initial;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-11-05 15:41:41 +05:30
|
|
|
initial = accounts(undefined, {});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
it('should be empty', () => expect(accounts(undefined, {}), 'to equal', {
|
2016-10-30 17:42:49 +05:30
|
|
|
active: null,
|
|
|
|
available: []
|
|
|
|
}));
|
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
it('should return last state if unsupported action', () =>
|
|
|
|
expect(accounts({state: 'foo'}, {}), 'to equal', {state: 'foo'})
|
|
|
|
);
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
describe(ACTIVATE, () => {
|
|
|
|
it('sets active account', () => {
|
2016-11-05 15:41:41 +05:30
|
|
|
expect(accounts(initial, activate(account)), 'to satisfy', {
|
2016-10-30 17:42:49 +05:30
|
|
|
active: account
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe(ADD, () => {
|
|
|
|
it('adds an account', () =>
|
2016-11-05 15:41:41 +05:30
|
|
|
expect(accounts(initial, add(account)), 'to satisfy', {
|
2016-10-30 17:42:49 +05:30
|
|
|
available: [account]
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2016-11-13 02:01:44 +05:30
|
|
|
it('should replace if account was added for the second time', () => {
|
|
|
|
const outdatedAccount = {
|
|
|
|
...account,
|
|
|
|
someShit: true
|
|
|
|
};
|
|
|
|
|
|
|
|
const updatedAccount = {
|
|
|
|
...account,
|
|
|
|
token: 'newToken'
|
|
|
|
};
|
|
|
|
|
|
|
|
return expect(
|
|
|
|
accounts({...initial, available: [outdatedAccount]}, add(updatedAccount)),
|
|
|
|
'to satisfy', {
|
|
|
|
available: [updatedAccount]
|
|
|
|
});
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
|
|
|
|
it('throws, when account is invalid', () => {
|
2016-11-05 15:41:41 +05:30
|
|
|
expect(() => accounts(initial, add()),
|
|
|
|
'to throw', 'Invalid or empty payload passed for accounts.add');
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe(REMOVE, () => {
|
|
|
|
it('should remove an account', () =>
|
2016-11-05 15:41:41 +05:30
|
|
|
expect(accounts({...initial, available: [account]}, remove(account)),
|
|
|
|
'to equal', initial)
|
2016-10-30 17:42:49 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
it('throws, when account is invalid', () => {
|
2016-11-05 15:41:41 +05:30
|
|
|
expect(() => accounts(initial, remove()),
|
|
|
|
'to throw', 'Invalid or empty payload passed for accounts.remove');
|
|
|
|
});
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2016-11-05 15:41:41 +05:30
|
|
|
describe(UPDATE_TOKEN, () => {
|
|
|
|
it('should update token', () => {
|
|
|
|
const newToken = 'newToken';
|
|
|
|
|
|
|
|
expect(accounts(
|
|
|
|
{active: account, available: [account]},
|
|
|
|
updateToken(newToken)
|
|
|
|
), 'to satisfy', {
|
|
|
|
active: {
|
|
|
|
...account,
|
|
|
|
token: newToken
|
|
|
|
},
|
|
|
|
available: [account]
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|