2024-01-16 13:01:31 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|
|
|
|
|
2024-01-17 14:09:12 +00:00
|
|
|
jest.mock( '@wordpress/api-fetch', () => jest.fn() );
|
|
|
|
|
2024-01-16 13:01:31 +00:00
|
|
|
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();
|
|
|
|
} );
|
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
it( 'should transit to preAssembleSite state when the url is /design', () => {
|
2024-01-16 13:01:31 +00:00
|
|
|
const hasStepInUrl = jest.fn( () => true );
|
|
|
|
const machine = designWithNoAiStateMachineDefinition.withConfig( {
|
|
|
|
guards: {
|
|
|
|
hasStepInUrl,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
const machineInterpret = interpret( machine ).start();
|
|
|
|
|
|
|
|
expect(
|
2024-01-18 14:52:49 +00:00
|
|
|
machineInterpret.getSnapshot().matches( 'preAssembleSite' )
|
2024-01-16 13:01:31 +00:00
|
|
|
).toBeTruthy();
|
|
|
|
} );
|
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
it( "should not transit to preAssembleSite state when the url isn't /design", () => {
|
2024-01-16 13:01:31 +00:00
|
|
|
const hasStepInUrl = jest.fn( () => false );
|
|
|
|
const machine = designWithNoAiStateMachineDefinition.withConfig( {
|
|
|
|
guards: {
|
|
|
|
hasStepInUrl,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
const machineInterpret = interpret( machine ).start();
|
|
|
|
|
|
|
|
expect(
|
2024-01-18 14:52:49 +00:00
|
|
|
machineInterpret.getSnapshot().matches( 'preAssembleSite' )
|
2024-01-16 13:01:31 +00:00
|
|
|
).toBeFalsy();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
describe( 'preAssembleSite state', () => {
|
2024-01-17 14:09:12 +00:00
|
|
|
it( 'should invoke `installAndActivateTheme` service', async () => {
|
2024-01-18 14:52:49 +00:00
|
|
|
const initialState =
|
|
|
|
'preAssembleSite.preApiCallLoader.installAndActivateTheme';
|
2024-01-17 14:09:12 +00:00
|
|
|
const installAndActivateThemeMock = jest.fn( () =>
|
|
|
|
Promise.resolve()
|
|
|
|
);
|
|
|
|
|
|
|
|
const machine = createMockMachine( {
|
|
|
|
services: {
|
|
|
|
installAndActivateTheme: installAndActivateThemeMock,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
const state = machine.getInitialState( initialState );
|
2024-01-17 14:09:12 +00:00
|
|
|
|
|
|
|
const actor = interpret( machine ).start( state );
|
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
await waitFor( actor, ( currentState ) => {
|
|
|
|
return currentState.matches(
|
|
|
|
'preAssembleSite.preApiCallLoader.installAndActivateTheme.pending'
|
|
|
|
);
|
|
|
|
} );
|
2024-01-17 14:09:12 +00:00
|
|
|
|
|
|
|
expect( installAndActivateThemeMock ).toHaveBeenCalled();
|
2024-01-18 14:52:49 +00:00
|
|
|
|
|
|
|
const finalState = await waitFor( actor, ( currentState ) => {
|
|
|
|
return currentState.matches(
|
|
|
|
'preAssembleSite.preApiCallLoader.installAndActivateTheme.success'
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
2024-01-17 14:09:12 +00:00
|
|
|
expect(
|
2024-01-18 14:52:49 +00:00
|
|
|
finalState.matches(
|
|
|
|
'preAssembleSite.preApiCallLoader.installAndActivateTheme.success'
|
|
|
|
)
|
2024-01-17 14:09:12 +00:00
|
|
|
).toBeTruthy();
|
|
|
|
} );
|
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
it( 'should invoke `assembleSite` service', async () => {
|
|
|
|
const initialState =
|
|
|
|
'preAssembleSite.preApiCallLoader.assembleSite';
|
|
|
|
|
|
|
|
const assembleSiteMock = jest.fn( () => Promise.resolve() );
|
|
|
|
|
|
|
|
const machine = createMockMachine( {
|
|
|
|
services: {
|
|
|
|
assembleSite: assembleSiteMock,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
|
|
|
const state = machine.getInitialState( initialState );
|
|
|
|
|
|
|
|
const actor = interpret( machine ).start( state );
|
|
|
|
|
|
|
|
await waitFor( actor, ( currentState ) => {
|
|
|
|
return currentState.matches(
|
|
|
|
'preAssembleSite.preApiCallLoader.assembleSite.pending'
|
|
|
|
);
|
|
|
|
} );
|
2024-01-16 13:01:31 +00:00
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
expect( assembleSiteMock ).toHaveBeenCalled();
|
|
|
|
|
|
|
|
const finalState = await waitFor( actor, ( currentState ) => {
|
|
|
|
return currentState.matches(
|
|
|
|
'preAssembleSite.preApiCallLoader.assembleSite.success'
|
|
|
|
);
|
|
|
|
} );
|
2024-01-16 13:01:31 +00:00
|
|
|
|
|
|
|
expect(
|
2024-01-18 14:52:49 +00:00
|
|
|
finalState.matches(
|
|
|
|
'preAssembleSite.preApiCallLoader.assembleSite.success'
|
|
|
|
)
|
2024-01-16 13:01:31 +00:00
|
|
|
).toBeTruthy();
|
|
|
|
} );
|
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
it( 'should invoke `createProducts` service', async () => {
|
|
|
|
const initialState =
|
|
|
|
'preAssembleSite.preApiCallLoader.createProducts';
|
|
|
|
|
|
|
|
const createProductsMock = jest.fn( () => Promise.resolve() );
|
2024-01-16 13:01:31 +00:00
|
|
|
|
|
|
|
const machine = createMockMachine( {
|
|
|
|
services: {
|
2024-01-18 14:52:49 +00:00
|
|
|
createProducts: createProductsMock,
|
2024-01-16 13:01:31 +00:00
|
|
|
},
|
|
|
|
} );
|
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
const state = machine.getInitialState( initialState );
|
2024-01-16 13:01:31 +00:00
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
const actor = interpret( machine ).start( state );
|
2024-01-16 13:01:31 +00:00
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
await waitFor( actor, ( currentState ) => {
|
|
|
|
return currentState.matches(
|
|
|
|
'preAssembleSite.preApiCallLoader.createProducts.pending'
|
|
|
|
);
|
|
|
|
} );
|
2024-01-16 13:01:31 +00:00
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
expect( createProductsMock ).toHaveBeenCalled();
|
|
|
|
|
|
|
|
const finalState = await waitFor( actor, ( currentState ) => {
|
|
|
|
return currentState.matches(
|
|
|
|
'preAssembleSite.preApiCallLoader.createProducts.success'
|
|
|
|
);
|
|
|
|
} );
|
2024-01-16 13:01:31 +00:00
|
|
|
|
|
|
|
expect(
|
2024-01-18 14:52:49 +00:00
|
|
|
finalState.matches(
|
|
|
|
'preAssembleSite.preApiCallLoader.createProducts.success'
|
|
|
|
)
|
2024-01-16 13:01:31 +00:00
|
|
|
).toBeTruthy();
|
|
|
|
} );
|
|
|
|
|
2024-01-18 14:52:49 +00:00
|
|
|
// We have to refactor this test with https://github.com/woocommerce/woocommerce/issues/43780
|
|
|
|
it.skip( 'should run `assignAPICallLoaderError` when `assembleSite` service fails', async () => {
|
2024-01-16 13:01:31 +00:00
|
|
|
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();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|