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