2019-12-08 00:32:00 +05:30
|
|
|
import expect from 'app/test/unexpected';
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
import { updateToken } from './actions';
|
2020-10-27 04:16:57 +05:30
|
|
|
import { add, remove, activate, reset, markAsDeleted } from './actions/pure-actions';
|
2020-01-18 02:07:52 +05:30
|
|
|
import { AccountsState } from './index';
|
2019-12-07 16:58:52 +05:30
|
|
|
import accounts, { Account } from './reducer';
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
const account: Account = {
|
2020-05-24 04:38:24 +05:30
|
|
|
id: 1,
|
|
|
|
username: 'username',
|
|
|
|
email: 'email@test.com',
|
|
|
|
token: 'foo',
|
2020-10-27 04:16:57 +05:30
|
|
|
refreshToken: '',
|
|
|
|
isDeleted: false,
|
|
|
|
};
|
2016-10-30 17:42:49 +05:30
|
|
|
|
|
|
|
describe('Accounts reducer', () => {
|
2020-05-24 04:38:24 +05:30
|
|
|
let initial: AccountsState;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
initial = accounts(undefined, {} as any);
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
2020-05-24 04:38:24 +05:30
|
|
|
|
|
|
|
it('should be empty', () =>
|
|
|
|
expect(accounts(undefined, {} as any), 'to equal', {
|
|
|
|
active: null,
|
|
|
|
available: [],
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should return last state if unsupported action', () =>
|
|
|
|
expect(accounts({ state: 'foo' } as any, {} as any), 'to equal', {
|
|
|
|
state: 'foo',
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('accounts:activate', () => {
|
|
|
|
it('sets active account', () => {
|
|
|
|
expect(accounts(initial, activate(account)), 'to satisfy', {
|
|
|
|
active: account.id,
|
|
|
|
});
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
describe('accounts: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],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws, when account is invalid', () => {
|
|
|
|
expect(
|
|
|
|
() =>
|
|
|
|
accounts(
|
|
|
|
initial,
|
|
|
|
// @ts-ignore
|
|
|
|
add(),
|
|
|
|
),
|
|
|
|
'to throw',
|
|
|
|
'Invalid or empty payload passed for accounts.add',
|
|
|
|
);
|
|
|
|
});
|
2016-11-05 15:41:41 +05:30
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
describe('accounts: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,
|
|
|
|
// @ts-ignore
|
|
|
|
remove(),
|
|
|
|
),
|
|
|
|
'to throw',
|
|
|
|
'Invalid or empty payload passed for accounts.remove',
|
|
|
|
);
|
|
|
|
});
|
2016-11-14 10:58:25 +05:30
|
|
|
});
|
2020-05-24 04:38:24 +05:30
|
|
|
|
|
|
|
describe('actions:reset', () => {
|
|
|
|
it('should reset accounts state', () =>
|
|
|
|
expect(accounts({ ...initial, available: [account] }, reset()), 'to equal', initial));
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
2020-05-24 04:38:24 +05:30
|
|
|
|
|
|
|
describe('accounts:updateToken', () => {
|
|
|
|
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
|
|
|
});
|
2020-10-27 04:16:57 +05:30
|
|
|
|
|
|
|
describe('accounts:markAsDeleted', () => {
|
|
|
|
it('should mark account as deleted', () => {
|
|
|
|
const isDeleted = true;
|
|
|
|
|
|
|
|
expect(accounts({ active: account.id, available: [account] }, markAsDeleted(isDeleted)), 'to satisfy', {
|
|
|
|
active: account.id,
|
|
|
|
available: [
|
|
|
|
{
|
|
|
|
...account,
|
|
|
|
isDeleted,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-10-30 17:42:49 +05:30
|
|
|
});
|