bigbuffet-rw/app/soapbox/reducers/__tests__/verification.test.ts

178 lines
4.5 KiB
TypeScript
Raw Normal View History

import { Map as ImmutableMap, Record as ImmutableRecord } from 'immutable';
2022-03-21 11:09:01 -07:00
import {
Challenge,
FETCH_CHALLENGES_SUCCESS,
FETCH_TOKEN_SUCCESS,
SET_CHALLENGES_COMPLETE,
SET_LOADING,
SET_NEXT_CHALLENGE,
} from 'soapbox/actions/verification';
2022-03-21 11:09:01 -07:00
import reducer from '../verification';
describe('verfication reducer', () => {
it('returns the initial state', () => {
expect(reducer(undefined, {} as any)).toMatchObject({
2022-03-21 11:09:01 -07:00
ageMinimum: null,
currentChallenge: null,
isLoading: false,
isComplete: false,
token: null,
instance: ImmutableMap(),
});
2022-03-21 11:09:01 -07:00
});
describe('FETCH_CHALLENGES_SUCCESS', () => {
it('sets the state', () => {
const state = ImmutableRecord({
2022-03-21 11:09:01 -07:00
ageMinimum: null,
currentChallenge: null,
isLoading: true,
isComplete: null,
token: null,
instance: ImmutableMap<string, any>(),
})();
2022-03-21 11:09:01 -07:00
const action = {
type: FETCH_CHALLENGES_SUCCESS,
ageMinimum: 13,
currentChallenge: 'email',
isComplete: false,
};
const expected = {
2022-03-21 11:09:01 -07:00
ageMinimum: 13,
currentChallenge: 'email',
isLoading: false,
isComplete: false,
token: null,
instance: ImmutableMap(),
};
2022-03-21 11:09:01 -07:00
expect(reducer(state, action)).toMatchObject(expected);
2022-03-21 11:09:01 -07:00
});
});
describe('FETCH_TOKEN_SUCCESS', () => {
it('sets the state', () => {
const state = ImmutableRecord({
ageMinimum: null,
currentChallenge: 'email' as Challenge,
2022-03-21 11:09:01 -07:00
isLoading: true,
isComplete: false,
2022-03-21 11:09:01 -07:00
token: null,
instance: ImmutableMap<string, any>(),
})();
2022-03-21 11:09:01 -07:00
const action = { type: FETCH_TOKEN_SUCCESS, value: '123' };
const expected = {
ageMinimum: null,
currentChallenge: 'email',
2022-03-21 11:09:01 -07:00
isLoading: false,
isComplete: false,
2022-03-21 11:09:01 -07:00
token: '123',
instance: ImmutableMap(),
};
2022-03-21 11:09:01 -07:00
expect(reducer(state, action)).toMatchObject(expected);
2022-03-21 11:09:01 -07:00
});
});
describe('SET_CHALLENGES_COMPLETE', () => {
it('sets the state', () => {
const state = ImmutableRecord({
ageMinimum: null,
currentChallenge: null,
2022-03-21 11:09:01 -07:00
isLoading: true,
isComplete: false,
token: null,
instance: ImmutableMap<string, any>(),
})();
2022-03-21 11:09:01 -07:00
const action = { type: SET_CHALLENGES_COMPLETE };
const expected = {
ageMinimum: null,
currentChallenge: null,
2022-03-21 11:09:01 -07:00
isLoading: false,
isComplete: true,
token: null,
instance: ImmutableMap(),
};
2022-03-21 11:09:01 -07:00
expect(reducer(state, action)).toMatchObject(expected);
2022-03-21 11:09:01 -07:00
});
});
describe('SET_NEXT_CHALLENGE', () => {
it('sets the state', () => {
const state = ImmutableRecord({
ageMinimum: null,
2022-03-21 11:09:01 -07:00
currentChallenge: null,
isLoading: true,
isComplete: false,
token: null,
instance: ImmutableMap<string, any>(),
})();
2022-03-21 11:09:01 -07:00
const action = {
type: SET_NEXT_CHALLENGE,
challenge: 'sms',
};
const expected = {
ageMinimum: null,
2022-03-21 11:09:01 -07:00
currentChallenge: 'sms',
isLoading: false,
isComplete: false,
token: null,
instance: ImmutableMap(),
};
2022-03-21 11:09:01 -07:00
expect(reducer(state, action)).toMatchObject(expected);
2022-03-21 11:09:01 -07:00
});
});
describe('SET_LOADING with no value', () => {
it('sets the state', () => {
const state = ImmutableRecord({
ageMinimum: null,
currentChallenge: null,
isLoading: false,
isComplete: false,
token: null,
instance: ImmutableMap<string, any>(),
})();
2022-03-21 11:09:01 -07:00
const action = { type: SET_LOADING };
const expected = {
ageMinimum: null,
currentChallenge: null,
isLoading: true,
isComplete: false,
token: null,
instance: ImmutableMap(),
};
2022-03-21 11:09:01 -07:00
expect(reducer(state, action)).toMatchObject(expected);
2022-03-21 11:09:01 -07:00
});
});
describe('SET_LOADING with a value', () => {
it('sets the state', () => {
const state = ImmutableRecord({
ageMinimum: null,
currentChallenge: null,
isLoading: true,
isComplete: false,
token: null,
instance: ImmutableMap<string, any>(),
})();
2022-03-21 11:09:01 -07:00
const action = { type: SET_LOADING, value: false };
const expected = {
ageMinimum: null,
currentChallenge: null,
isLoading: false,
isComplete: false,
token: null,
instance: ImmutableMap(),
};
2022-03-21 11:09:01 -07:00
expect(reducer(state, action)).toMatchObject(expected);
2022-03-21 11:09:01 -07:00
});
});
});