Skip to content

Commit 97c7132

Browse files
Mario Aguiarravichdev
authored andcommitted
Add tests for utility functions
1 parent c2e5edf commit 97c7132

File tree

5 files changed

+96
-27
lines changed

5 files changed

+96
-27
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2021 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+
export * from './update';
18+
export * from './promises';

plugin/assets/src/settings/utils.js renamed to plugin/assets/src/settings/utils/promises.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,13 @@ import apiFetch from '@wordpress/api-fetch';
2222
/**
2323
* Internal dependencies
2424
*/
25-
import { UPDATERS } from './constants';
26-
import getConfig from '../admin/get-config';
27-
28-
/**
29-
* Handles each kind of update
30-
*
31-
* @param {*} type Item to update
32-
*/
33-
export const update = type => {
34-
if ( type === UPDATERS.FONTS.type ) {
35-
return updateFonts();
36-
}
37-
38-
if ( type === UPDATERS.ICONS.type ) {
39-
return updateIcons();
40-
}
41-
42-
if ( isCoreUpdate( type ) ) {
43-
return new Promise( () => {
44-
window.location.href = getConfig( 'coreUpdateUrl' );
45-
} );
46-
}
47-
};
25+
import { UPDATERS } from '../constants';
26+
import getConfig from '../../admin/get-config';
4827

4928
/**
5029
* Update fonts.
5130
*/
52-
const updateFonts = () => {
31+
export const updateFonts = () => {
5332
return new Promise( ( resolve, reject ) => {
5433
apiFetch( {
5534
path: getConfig( 'assetsRestPath' ) + 'retrieve-fonts/force',
@@ -66,7 +45,7 @@ const updateFonts = () => {
6645
/**
6746
* Update icons.
6847
*/
69-
const updateIcons = () => {
48+
export const updateIcons = () => {
7049
return new Promise( ( resolve, reject ) => {
7150
apiFetch( {
7251
path: getConfig( 'assetsRestPath' ) + 'retrieve-icons/force',
@@ -80,7 +59,7 @@ const updateIcons = () => {
8059
} );
8160
};
8261

83-
const isCoreUpdate = type =>
62+
export const isCoreUpdate = type =>
8463
[ UPDATERS.PLUGIN.type, UPDATERS.THEME.type ].includes( type );
8564

8665
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { UPDATERS } from '../constants';
5+
import getConfig from '../../admin/get-config';
6+
import { updateFonts, updateIcons, isCoreUpdate } from './promises';
7+
8+
/**
9+
* Handles each kind of update
10+
*
11+
* @param {*} type Item to update
12+
*/
13+
export const update = type => {
14+
if ( type === UPDATERS.FONTS.type ) {
15+
return updateFonts();
16+
}
17+
18+
if ( type === UPDATERS.ICONS.type ) {
19+
return updateIcons();
20+
}
21+
22+
if ( isCoreUpdate( type ) ) {
23+
return new Promise( () => {
24+
window.location.href = getConfig( 'coreUpdateUrl' );
25+
} );
26+
}
27+
};

plugin/tests/js/settings/reducer.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const initialState = {
3737
errors: {},
3838
};
3939

40-
describe( 'Reducer', () => {
40+
describe( 'Settings: Reducer', () => {
4141
it( 'should return default state', () => {
4242
const action = {
4343
type: null,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
jest.mock( '../../../../assets/src/settings/utils/promises', () => ( {
20+
updateFonts: jest.fn(),
21+
updateIcons: jest.fn(),
22+
} ) );
23+
24+
/**
25+
* Internal dependencies
26+
*/
27+
import { update } from '../../../../assets/src/settings/utils/update';
28+
import {
29+
updateFonts,
30+
updateIcons,
31+
} from '../../../../assets/src/settings/utils/promises';
32+
33+
describe( 'Settings: Update', () => {
34+
it( 'should call fonts', () => {
35+
update( 'FONTS' );
36+
37+
expect( updateFonts ).toHaveBeenCalledWith();
38+
} );
39+
40+
it( 'should call icons', () => {
41+
update( 'ICONS' );
42+
43+
expect( updateIcons ).toHaveBeenCalledWith();
44+
} );
45+
} );

0 commit comments

Comments
 (0)