Skip to content

Commit dc4a108

Browse files
Mario Aguiarravichdev
authored andcommitted
Add tests for reducer
1 parent f721f6a commit dc4a108

File tree

2 files changed

+184
-2
lines changed

2 files changed

+184
-2
lines changed

plugin/tests/js/settings/components/integrations/__snapshots__/updater.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exports[`Settings: Updater matches snapshot 1`] = `
2222
<p
2323
class="mdc-typography--body1"
2424
>
25-
Last update on Apr 6, 2021, 11:33 PM
25+
Last update on Apr 7, 2021, 09:47 PM
2626
</p>
2727
</div>
2828
<div
@@ -255,7 +255,7 @@ exports[`Settings: Updater matches snapshot when should not update 1`] = `
255255
<p
256256
class="mdc-typography--body1"
257257
>
258-
Last update on Apr 6, 2021, 11:33 PM
258+
Last update on Apr 7, 2021, 09:47 PM
259259
</p>
260260
</div>
261261
<div
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/**
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import '@testing-library/jest-dom/extend-expect';
18+
19+
/**
20+
* Internal dependencies
21+
*/
22+
import { reducer } from '../../../assets/src/settings/reducer';
23+
24+
const initialState = {
25+
status: 'idle',
26+
apiStatus: 'ok',
27+
updaters: {
28+
FONTS: {
29+
title: 'Google Fonts',
30+
type: 'FONTS',
31+
lastUpdated: Date.now(),
32+
needsKey: true,
33+
updateAvailable: true,
34+
autoUpdates: true,
35+
},
36+
},
37+
errors: {},
38+
};
39+
40+
describe( 'Reducer', () => {
41+
it( 'should return default state', () => {
42+
const action = {
43+
type: null,
44+
payload: null,
45+
};
46+
47+
const result = reducer( initialState, action );
48+
49+
expect( result ).toStrictEqual( initialState );
50+
} );
51+
52+
it( 'should toggle auto updates', () => {
53+
const action = {
54+
type: 'TOGGLE_UPDATES',
55+
payload: { type: 'FONTS' },
56+
};
57+
58+
const result = reducer( initialState, action );
59+
60+
expect( result.updaters.FONTS.autoUpdates ).toStrictEqual( false );
61+
} );
62+
63+
it( 'should remove API key', () => {
64+
const action = {
65+
type: 'REMOVE_API_KEY',
66+
};
67+
68+
const result = reducer( initialState, action );
69+
70+
expect( result.apiStatus ).toStrictEqual( 'install' );
71+
} );
72+
73+
it( 'should add API key', () => {
74+
const lastUpdated = Date.now();
75+
76+
const action = {
77+
type: 'ADD_API_KEY',
78+
payload: { lastUpdated },
79+
};
80+
81+
const result = reducer( initialState, action );
82+
83+
expect( result.apiStatus ).toStrictEqual( 'ok' );
84+
expect( result.updaters.FONTS.updateAvailable ).toStrictEqual( false );
85+
expect( result.updaters.FONTS.lastUpdated ).toStrictEqual( lastUpdated );
86+
} );
87+
88+
it( 'should set last updated', () => {
89+
const lastUpdated = Date.now();
90+
91+
const action = {
92+
type: 'SET_UPDATED',
93+
payload: {
94+
lastUpdated,
95+
type: 'FONTS',
96+
},
97+
};
98+
99+
const result = reducer( initialState, action );
100+
101+
expect( result.updaters.FONTS.updateAvailable ).toStrictEqual( false );
102+
expect( result.updaters.FONTS.lastUpdated ).toStrictEqual( lastUpdated );
103+
} );
104+
105+
it( 'should add error', () => {
106+
const action = {
107+
type: 'ADD_ERROR',
108+
payload: {
109+
id: 'api_error',
110+
error: {
111+
message: 'example error',
112+
},
113+
},
114+
};
115+
116+
const result = reducer( initialState, action );
117+
118+
expect( result.errors ).toHaveProperty( 'api_error' );
119+
} );
120+
121+
it( 'should remove error', () => {
122+
const state = {
123+
status: 'idle',
124+
apiStatus: 'ok',
125+
updaters: {
126+
FONTS: {
127+
title: 'Google Fonts',
128+
type: 'FONTS',
129+
lastUpdated: Date.now(),
130+
needsKey: true,
131+
updateAvailable: true,
132+
autoUpdates: true,
133+
},
134+
},
135+
errors: {
136+
api_error: {
137+
message: 'Test Error',
138+
}
139+
},
140+
};
141+
142+
const action = {
143+
type: 'REMOVE_ERROR',
144+
payload: { id: 'api_error' },
145+
};
146+
147+
const result = reducer( state, action );
148+
149+
expect( result.errors ).not.toHaveProperty( 'api_error' );
150+
} );
151+
152+
it( 'should remove all errors', () => {
153+
const state = {
154+
status: 'idle',
155+
apiStatus: 'ok',
156+
updaters: {
157+
FONTS: {
158+
title: 'Google Fonts',
159+
type: 'FONTS',
160+
lastUpdated: Date.now(),
161+
needsKey: true,
162+
updateAvailable: true,
163+
autoUpdates: true,
164+
},
165+
},
166+
errors: {
167+
api_error: {
168+
message: 'Test Error',
169+
}
170+
},
171+
};
172+
173+
const action = {
174+
type: 'CLEAR_ERRORS',
175+
payload: { id: 'api_error' },
176+
};
177+
178+
const result = reducer( state, action );
179+
180+
expect( result.errors ).toStrictEqual( {} );
181+
} );
182+
} );

0 commit comments

Comments
 (0)