Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update connection status tests to typescript #37829

Merged
merged 9 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import '@testing-library/jest-dom';
import { CONNECTION_STORE_ID } from '@automattic/jetpack-connection';
import { jest } from '@jest/globals';
import { render, renderHook, screen } from '@testing-library/react';
import { useSelect } from '@wordpress/data';
import ConnectionStatusCard from '../index';
import type { StateProducts, MyJetpackInitialState } from '../../../data/types';

interface TestMyJetpackInitialState {
lifecycleStats: Pick<
MyJetpackInitialState[ 'lifecycleStats' ],
'historicallyActiveModules' | 'brokenModules'
>;
products: {
items: {
'anti-spam': Pick<
StateProducts[ 'anti-spam' ],
'requires_user_connection' | 'status' | 'pricing_for_ui'
>;
};
};
}

const resetInitialState = () => {
global.window.myJetpackInitialState = {
( window.myJetpackInitialState as unknown as TestMyJetpackInitialState ) = {
lifecycleStats: {
historicallyActiveModules: [],
brokenModules: {
Expand All @@ -18,8 +34,17 @@ const resetInitialState = () => {
'anti-spam': {
requires_user_connection: false,
status: 'inactive',
pricingForUi: {
productTerm: 'year',
// This property is needed as it is used when the `useAllProducts` hook is called
// in the connection status card component
pricing_for_ui: {
product_term: 'year',
available: false,
wpcom_product_slug: '',
currency_code: '',
full_price: 0,
discount_price: 0,
coupon_discount: 0,
is_introductory_offer: false,
},
},
},
Expand All @@ -33,7 +58,7 @@ const setConnectionStore = ( {
hasConnectedOwner = false,
} = {} ) => {
let storeSelect;
renderHook( () => useSelect( select => ( storeSelect = select( CONNECTION_STORE_ID ) ) ) );
renderHook( () => useSelect( select => ( storeSelect = select( CONNECTION_STORE_ID ) ), [] ) );
jest
.spyOn( storeSelect, 'getConnectionStatus' )
.mockReset()
Expand Down Expand Up @@ -75,8 +100,8 @@ describe( 'ConnectionStatusCard', () => {

describe( 'When the site is not registered and has broken modules', () => {
const setup = () => {
global.window.myJetpackInitialState.lifecycleStats.brokenModules.needs_site_connection = [
'module1',
window.myJetpackInitialState.lifecycleStats.brokenModules.needs_site_connection = [
'anti-spam',
];
return render( <ConnectionStatusCard { ...testProps } /> );
};
Expand Down Expand Up @@ -119,9 +144,7 @@ describe( 'ConnectionStatusCard', () => {
describe( 'There are products that require user connection', () => {
const setup = () => {
setConnectionStore( { isRegistered: true } );
global.window.myJetpackInitialState.products.items[
'anti-spam'
].requires_user_connection = true;
window.myJetpackInitialState.products.items[ 'anti-spam' ].requires_user_connection = true;
return render( <ConnectionStatusCard { ...testProps } /> );
};

Expand All @@ -142,8 +165,8 @@ describe( 'ConnectionStatusCard', () => {
describe( 'When the user has not connected their WordPress.com account and there are broken modules', () => {
const setup = () => {
setConnectionStore( { isRegistered: true } );
global.window.myJetpackInitialState.lifecycleStats.brokenModules.needs_user_connection = [
'module1',
window.myJetpackInitialState.lifecycleStats.brokenModules.needs_user_connection = [
'anti-spam',
];
return render( <ConnectionStatusCard { ...testProps } /> );
};
Expand Down
3 changes: 2 additions & 1 deletion projects/packages/my-jetpack/_inc/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export type BackupCountStats = {
total_audio_count: number;
};

type StateProducts = Window[ 'myJetpackInitialState' ][ 'products' ][ 'items' ];
export type MyJetpackInitialState = Window[ 'myJetpackInitialState' ];
export type StateProducts = Window[ 'myJetpackInitialState' ][ 'products' ][ 'items' ];
export type ProductSnakeCase = StateProducts[ string ];

export type ProductCamelCase = ToCamelCase< ProductSnakeCase > & {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Update connection status card tests to TS
1 change: 1 addition & 0 deletions projects/packages/my-jetpack/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare module '@wordpress/components';
declare module '@wordpress/compose';
declare module '@wordpress/i18n';
declare module '@wordpress/icons';
declare module '@automattic/jetpack-connection';

type JetpackModule =
| 'anti-spam'
Expand Down
3 changes: 3 additions & 0 deletions projects/packages/my-jetpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@
"@automattic/jetpack-webpack-config": "workspace:*",
"@babel/core": "7.24.7",
"@babel/preset-env": "7.24.7",
"@jest/globals": "29.7.0",
"@storybook/react": "8.1.6",
"@testing-library/dom": "10.1.0",
"@testing-library/jest-dom": "6.4.2",
"@testing-library/react": "15.0.7",
"@testing-library/user-event": "14.5.2",
"@types/jest": "29.5.12",
"@types/react": "18.3.1",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
Expand Down
Loading