accounts-frontend/packages/app/components/accounts/reducer.test.ts

163 lines
3.3 KiB
TypeScript
Raw Normal View History

import expect from 'app/test/unexpected';
2019-12-07 16:58:52 +05:30
import { updateToken } from './actions';
2016-11-05 15:41:41 +05:30
import {
add,
remove,
activate,
reset,
ADD,
REMOVE,
ACTIVATE,
UPDATE_TOKEN,
RESET,
2019-12-07 16:58:52 +05:30
} from './actions/pure-actions';
import accounts, { Account } from './reducer';
2019-12-07 16:58:52 +05:30
const account: Account = {
id: 1,
username: 'username',
email: 'email@test.com',
token: 'foo',
2019-12-07 16:58:52 +05:30
} as Account;
describe('Accounts reducer', () => {
let initial;
beforeEach(() => {
2019-12-07 16:58:52 +05:30
initial = accounts(undefined, {} as any);
});
it('should be empty', () =>
2019-12-07 16:58:52 +05:30
expect(accounts(undefined, {} as any), 'to equal', {
active: null,
available: [],
}));
it('should return last state if unsupported action', () =>
2019-12-07 16:58:52 +05:30
expect(accounts({ state: 'foo' } as any, {} as any), 'to equal', {
state: 'foo',
}));
2016-11-05 15:41:41 +05:30
describe(ACTIVATE, () => {
it('sets active account', () => {
expect(accounts(initial, activate(account)), 'to satisfy', {
active: account.id,
});
});
});
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],
},
);
});
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
});
it('throws, when account is invalid', () => {
expect(
2019-12-07 16:58:52 +05:30
() =>
accounts(
initial,
// @ts-ignore
add(),
),
'to throw',
'Invalid or empty payload passed for accounts.add',
);
2016-11-14 10:58:25 +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(
2019-12-07 16:58:52 +05:30
() =>
accounts(
initial,
// @ts-ignore
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,
},
],
},
);
});
});
});