|
| 1 | +import { Cmd, getCmd, getModel } from 'redux-loop' |
| 2 | +import { set as setToDb, get as getFromDb } from 'idb-keyval' |
| 3 | + |
| 4 | +import { State } from '../types' |
| 5 | +import { GLOBAL_LANGUAGE_DB_KEY } from '../constants' |
| 6 | +import { actions } from '../actions' |
| 7 | + |
| 8 | +import { initialState, Reducer } from '../reducer' |
| 9 | + |
| 10 | +const mockSelectedState: State = initialState |
| 11 | + |
| 12 | +describe('Settings | Expansions | GlobalLanguage | reducer', () => { |
| 13 | + it('should return the initial state', () => { |
| 14 | + // @ts-ignore |
| 15 | + const result = Reducer(undefined, {}) |
| 16 | + |
| 17 | + expect(result).toEqual(initialState) |
| 18 | + }) |
| 19 | + |
| 20 | + it('should handle SELECT', () => { |
| 21 | + const result = Reducer(mockSelectedState, actions.select('PL')) |
| 22 | + |
| 23 | + const expected = 'PL' |
| 24 | + |
| 25 | + const model = getModel(result) |
| 26 | + const cmd = getCmd(result) |
| 27 | + |
| 28 | + expect(model).toEqual(expected) |
| 29 | + |
| 30 | + expect(cmd).toEqual( |
| 31 | + Cmd.run(setToDb, { |
| 32 | + args: [GLOBAL_LANGUAGE_DB_KEY, expected], |
| 33 | + successActionCreator: actions.setToDBSuccessful, |
| 34 | + failActionCreator: actions.setToDBFailed, |
| 35 | + }) |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should handle FETCH_FROM_DB', () => { |
| 40 | + const result = Reducer(initialState, actions.fetchFromDB()) |
| 41 | + |
| 42 | + const model = getModel(result) |
| 43 | + const cmd = getCmd(result) |
| 44 | + |
| 45 | + expect(model).toEqual(initialState) |
| 46 | + |
| 47 | + expect(cmd).toEqual( |
| 48 | + Cmd.run(getFromDb, { |
| 49 | + args: [GLOBAL_LANGUAGE_DB_KEY], |
| 50 | + successActionCreator: actions.fetchFromDBSuccessful, |
| 51 | + failActionCreator: actions.fetchFromDBFailed, |
| 52 | + }) |
| 53 | + ) |
| 54 | + }) |
| 55 | + |
| 56 | + it('should handle FETCH_FROM_DB_SUCCESS for defined state', () => { |
| 57 | + const result = Reducer( |
| 58 | + mockSelectedState, |
| 59 | + actions.fetchFromDBSuccessful(initialState) |
| 60 | + ) |
| 61 | + |
| 62 | + expect(getModel(result)).toEqual(mockSelectedState) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should handle FETCH_FROM_DB_SUCCESS for undefined state', () => { |
| 66 | + const result = Reducer( |
| 67 | + initialState, |
| 68 | + // @ts-ignore |
| 69 | + actions.fetchFromDBSuccessful(undefined) |
| 70 | + ) |
| 71 | + |
| 72 | + expect(getModel(result)).toEqual(initialState) |
| 73 | + }) |
| 74 | + |
| 75 | + it.todo('should handle FETCH_FROM_DB_FAILURE') |
| 76 | + it.todo('should handle SET_TO_DB') |
| 77 | + it.todo('should handle SET_TO_DB_SUCCESS') |
| 78 | + it.todo('should handle SET_TO_DB_FAILURE') |
| 79 | +}) |
0 commit comments