Skip to content

Commit baf2fb7

Browse files
authored
Merge pull request #86 from material-components/feature/534-settings-page-tests-2
Add tests for settings page
2 parents cbb8854 + 2597732 commit baf2fb7

File tree

18 files changed

+1242
-36
lines changed

18 files changed

+1242
-36
lines changed

plugin/assets/fonts/icons.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

plugin/assets/src/settings/components/integrations/api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ import Button from '../../../wizard/components/navigation/button';
3030
import { setApiKey } from '../../utils';
3131
import Dialog from '../../../common/components/dialog';
3232

33-
const Api = () => {
34-
const { state, dispatch } = useContext( SettingsContext );
33+
const Api = ( { apiStatus } ) => {
34+
const { dispatch } = useContext( SettingsContext );
3535
const [ api, setApi ] = useState( null );
3636
const [ isLoading, setIsLoading ] = useState( false );
3737
const [ confirm, setConfirm ] = useState( false );
38-
const isApiOk = 'ok' === state.apiStatus;
38+
const isApiOk = 'ok' === apiStatus;
3939

4040
const dispatchError = error =>
4141
dispatch( {

plugin/assets/src/settings/components/integrations/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ const Integrations = () => {
8080
type={ UPDATERS[ key ].type }
8181
displayUpdatedOn={ UPDATERS[ key ].displayUpdatedOn }
8282
versionAvailable={ UPDATERS[ key ].versionAvailable }
83+
apiStatus={ state.apiStatus }
84+
updateAvailable={
85+
state.updaters[ UPDATERS[ key ].type ].updateAvailable
86+
}
8387
/>
8488
) ) }
8589
</div>
@@ -104,7 +108,7 @@ const Integrations = () => {
104108
} }
105109
></p>
106110

107-
<Api />
111+
<Api apiStatus={ state.apiStatus } />
108112
</div>
109113
);
110114
};

plugin/assets/src/settings/components/integrations/updater.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@ const Updater = ( {
4444
type,
4545
displayUpdatedOn,
4646
versionAvailable,
47+
apiStatus,
48+
updateAvailable,
4749
} ) => {
4850
const [ id ] = useState( _uniqueId( 'updater-' ) );
49-
const { state, dispatch } = useContext( SettingsContext );
50-
const isDisabled = needsKey && 'ok' !== state.apiStatus;
51+
const { dispatch } = useContext( SettingsContext );
52+
const isDisabled = needsKey && 'ok' !== apiStatus;
5153
const updatedDate = lastUpdated
5254
? dateI18n( 'M j, Y, h:i A', lastUpdated )
5355
: __( 'never', 'material-design' );
5456
const [ isUpdating, setIsUpdating ] = useState( false );
55-
const shouldUpdate = ! isDisabled && state.updaters[ type ].updateAvailable;
56-
const shouldNotUpdate =
57-
! isDisabled && ! state.updaters[ type ].updateAvailable;
57+
const shouldUpdate = ! isDisabled && updateAvailable;
58+
const shouldNotUpdate = ! isDisabled && ! updateAvailable;
5859

5960
const handleUpdate = response => {
6061
setIsUpdating( true );
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+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Settings: Errors matches snapshot 1`] = `<div />`;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
/**
18+
* External dependencies
19+
*/
20+
import '@testing-library/jest-dom/extend-expect';
21+
import { render } from '@testing-library/react';
22+
23+
/**
24+
* Internal dependencies
25+
*/
26+
import Errors from '../../../../assets/src/settings/components/errors';
27+
import { SettingsProvider } from '../../../../assets/src/settings/context';
28+
29+
const setup = () => {
30+
return render(
31+
<SettingsProvider>
32+
<Errors />
33+
</SettingsProvider>
34+
);
35+
};
36+
37+
describe( 'Settings: Errors', () => {
38+
it( 'matches snapshot', () => {
39+
const { container } = setup();
40+
expect( container ).toMatchSnapshot();
41+
} );
42+
} );

0 commit comments

Comments
 (0)