blob: 93f0e0a3c87f6eda404d4a59dace2f726520f3dc [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import {assert} from 'chai';
6import {createRequestReducer,
7 createKeyedRequestReducer} from './redux-helpers.js';
8
9let keyedRequestReducer;
10let requestReducer;
11
12describe('redux-helpers', () => {
13 describe('createKeyedRequestReducer', () => {
14 beforeEach(() => {
15 keyedRequestReducer = createKeyedRequestReducer(
16 'REQUEST_START', 'REQUEST_SUCCESS', 'REQUEST_FAILURE');
17 });
18
19 it('sets requesting to true on start', () => {
20 assert.deepEqual(keyedRequestReducer({}, {type: 'REQUEST_START'}),
21 {['*']: {requesting: true, error: null}});
22 });
23
24 it('sets requesting to false on success', () => {
25 assert.deepEqual(keyedRequestReducer({}, {type: 'REQUEST_SUCCESS'}),
26 {['*']: {requesting: false, error: null}});
27 });
28
29 it('sets error message on failure', () => {
30 assert.deepEqual(keyedRequestReducer({}, {
31 type: 'REQUEST_FAILURE',
32 error: 'hello',
33 }), {['*']: {requesting: false, error: 'hello'}});
34 });
35
36 it('preserves previous request state on start', () => {
37 const initialState = {
38 ['*']: {requesting: false, error: 'hello'},
39 };
40 assert.deepEqual(keyedRequestReducer(initialState, {
41 type: 'REQUEST_START',
42 requestKey: 'chromium:11',
43 }), {
44 ['*']: {requesting: false, error: 'hello'},
45 ['chromium:11']: {requesting: true, error: null},
46 });
47 });
48
49 it('preserves previous request state on success', () => {
50 const initialState = {
51 ['*']: {requesting: false, error: 'hello'},
52 ['chromium:11']: {requesting: true, error: null},
53 };
54 assert.deepEqual(keyedRequestReducer(initialState, {
55 type: 'REQUEST_SUCCESS',
56 requestKey: 'chromium:11',
57 }), {
58 ['*']: {requesting: false, error: 'hello'},
59 ['chromium:11']: {requesting: false, error: null},
60 });
61 });
62
63 it('preserves previous request state on failure', () => {
64 const initialState = {
65 ['*']: {requesting: false, error: 'hello'},
66 ['chromium:11']: {requesting: false, error: null},
67 };
68 assert.deepEqual(keyedRequestReducer(initialState, {
69 type: 'REQUEST_FAILURE',
70 requestKey: 'chromium:11',
71 error: 'something went wrong',
72 }), {
73 ['*']: {requesting: false, error: 'hello'},
74 ['chromium:11']: {requesting: false, error: 'something went wrong'},
75 });
76 });
77 });
78
79 describe('createRequestReducer', () => {
80 beforeEach(() => {
81 requestReducer = createRequestReducer(
82 'REQUEST_START', 'REQUEST_SUCCESS', 'REQUEST_FAILURE');
83 });
84
85 it('sets requesting to true on start', () => {
86 assert.deepEqual(requestReducer({}, {type: 'REQUEST_START'}),
87 {requesting: true, error: null});
88 });
89
90 it('sets requesting to false on success', () => {
91 assert.deepEqual(requestReducer({}, {type: 'REQUEST_SUCCESS'}),
92 {requesting: false, error: null});
93 });
94
95 it('sets error message on failure', () => {
96 assert.deepEqual(requestReducer({}, {
97 type: 'REQUEST_FAILURE',
98 error: 'hello',
99 }), {requesting: false, error: 'hello'});
100 });
101 });
102});