From dc0fd26917a662a1700b2a33e06a613ba397d2a6 Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Tue, 16 Jan 2024 14:01:31 +0100 Subject: [PATCH] CYS - Core: add unit test for the `NoAI` flow state machine (#43692) * CYS - Core: add unit test * Add changefile(s) from automation for the following project(s): woocommerce * fix description --------- Co-authored-by: github-actions --- .../design-without-ai/state-machine.tsx | 4 +- .../tests/state-machine.test.ts | 151 ++++++++++++++++++ ...st-for-the-state-machine-for-the-noai-flow | 4 + 3 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce-admin/client/customize-store/design-without-ai/tests/state-machine.test.ts create mode 100644 plugins/woocommerce/changelog/43692-43689-cys-core-add-unit-test-for-the-state-machine-for-the-noai-flow diff --git a/plugins/woocommerce-admin/client/customize-store/design-without-ai/state-machine.tsx b/plugins/woocommerce-admin/client/customize-store/design-without-ai/state-machine.tsx index d497d7e0b14..bb0ec3ffa5f 100644 --- a/plugins/woocommerce-admin/client/customize-store/design-without-ai/state-machine.tsx +++ b/plugins/woocommerce-admin/client/customize-store/design-without-ai/state-machine.tsx @@ -66,7 +66,8 @@ export const designWithNoAiStateMachineDefinition = createMachine( ], }, preAssembleSite: { - type: 'parallel', + id: 'preAssembleSite', + initial: 'assembleSite', states: { assembleSite: { initial: 'pending', @@ -93,6 +94,7 @@ export const designWithNoAiStateMachineDefinition = createMachine( }, }, showAssembleHub: { + id: 'showAssembleHub', meta: { component: AssembleHubLoader, }, diff --git a/plugins/woocommerce-admin/client/customize-store/design-without-ai/tests/state-machine.test.ts b/plugins/woocommerce-admin/client/customize-store/design-without-ai/tests/state-machine.test.ts new file mode 100644 index 00000000000..6ab25608146 --- /dev/null +++ b/plugins/woocommerce-admin/client/customize-store/design-without-ai/tests/state-machine.test.ts @@ -0,0 +1,151 @@ +/** + * External dependencies + */ +import { interpret } from 'xstate'; +import { waitFor } from 'xstate/lib/waitFor'; + +/** + * Internal dependencies + */ +import { designWithNoAiStateMachineDefinition } from '../state-machine'; + +const createMockMachine = ( { + services, + guards, + actions, +}: Parameters< + typeof designWithNoAiStateMachineDefinition.withConfig +>[ 0 ] ) => { + const machineWithConfig = designWithNoAiStateMachineDefinition.withConfig( { + services, + guards, + actions, + } ); + + return machineWithConfig; +}; + +describe( 'Design Without AI state machine', () => { + beforeEach( () => { + jest.clearAllMocks(); + } ); + + describe( 'navigate state', () => { + it( 'should start with the navigate state', async () => { + const expectedValue = 'navigate'; + + const actualState = + designWithNoAiStateMachineDefinition.initialState; + + expect( actualState.matches( expectedValue ) ).toBeTruthy(); + } ); + + it( 'should check the url', () => { + const hasStepInUrl = jest.fn( () => true ); + const machine = designWithNoAiStateMachineDefinition.withConfig( { + guards: { + hasStepInUrl, + }, + } ); + + interpret( machine ).start(); + + expect( hasStepInUrl ).toBeCalled(); + } ); + + it( 'should transit to preAssembleSite state when the url is /design', () => { + const hasStepInUrl = jest.fn( () => true ); + const machine = designWithNoAiStateMachineDefinition.withConfig( { + guards: { + hasStepInUrl, + }, + } ); + + const machineInterpret = interpret( machine ).start(); + + expect( + machineInterpret.getSnapshot().matches( 'preAssembleSite' ) + ).toBeTruthy(); + } ); + + it( "should not transit to preAssembleSite state when the url isn't /design", () => { + const hasStepInUrl = jest.fn( () => false ); + const machine = designWithNoAiStateMachineDefinition.withConfig( { + guards: { + hasStepInUrl, + }, + } ); + + const machineInterpret = interpret( machine ).start(); + + expect( + machineInterpret.getSnapshot().matches( 'preAssembleSite' ) + ).toBeFalsy(); + } ); + } ); + + describe( 'the preAssembleSite state', () => { + const initialState = 'preAssembleSite'; + it( 'should start with the pending state', async () => { + const machine = createMockMachine( {} ); + + const actor = interpret( machine ).start( initialState ); + + expect( + actor.getSnapshot().matches( 'preAssembleSite.assembleSite' ) + ).toBeTruthy(); + } ); + + it( 'should invoke `assembleSite` service', async () => { + const assembleSiteMock = jest.fn( () => Promise.resolve() ); + + const machine = createMockMachine( { + services: { + assembleSite: assembleSiteMock, + }, + } ); + + const state = machine.getInitialState( 'preAssembleSite' ); + + const actor = interpret( machine ); + + const services = actor.start( state ); + + await waitFor( services, ( currentState ) => + currentState.matches( 'preAssembleSite.assembleSite.pending' ) + ); + + expect( assembleSiteMock ).toHaveBeenCalled(); + expect( + actor.getSnapshot().matches( 'showAssembleHub' ) + ).toBeTruthy(); + } ); + + it( 'should run `assignAPICallLoaderError` when `assembleSite` service fails', async () => { + const assembleSiteMock = jest.fn( () => Promise.reject() ); + const assignAPICallLoaderErrorMock = jest.fn(); + + const machine = createMockMachine( { + services: { + assembleSite: assembleSiteMock, + }, + actions: { + assignAPICallLoaderError: assignAPICallLoaderErrorMock, + }, + } ); + + const state = machine.getInitialState( 'preAssembleSite' ); + + const actor = interpret( machine ); + + const services = actor.start( state ); + + await waitFor( services, ( currentState ) => + currentState.matches( 'preAssembleSite.assembleSite.pending' ) + ); + + expect( assembleSiteMock ).toHaveBeenCalled(); + expect( assignAPICallLoaderErrorMock ).toHaveBeenCalled(); + } ); + } ); +} ); diff --git a/plugins/woocommerce/changelog/43692-43689-cys-core-add-unit-test-for-the-state-machine-for-the-noai-flow b/plugins/woocommerce/changelog/43692-43689-cys-core-add-unit-test-for-the-state-machine-for-the-noai-flow new file mode 100644 index 00000000000..a320d6288ed --- /dev/null +++ b/plugins/woocommerce/changelog/43692-43689-cys-core-add-unit-test-for-the-state-machine-for-the-noai-flow @@ -0,0 +1,4 @@ +Significance: patch +Type: dev +Comment: CYS - Core: add unit test for the `NoAI` flow state machine +