-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
458a663
commit c09a572
Showing
7 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "@frontend/state-machine", | ||
"version": "1.0.0", | ||
"main": "src/index.ts", | ||
"types": "dist/index.d.ts", | ||
"esbuild": "src/index.ts", | ||
"license": "None", | ||
"dependencies": { | ||
"xstate": "^4.22.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './page' | ||
export * from './search' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { interpret } from 'xstate' | ||
import { pageMachine } from './page' | ||
import { searchModel } from './search' | ||
|
||
const fetchSearchResults = jest.fn() | ||
|
||
const pageMachineWithMocks = pageMachine.withConfig({ | ||
services: { | ||
fetchSearchResults, | ||
}, | ||
}) | ||
|
||
it('when the user types in a search, loads that search', (done) => { | ||
const mockData = { | ||
searchUsers: { | ||
resultsCount: 1, | ||
results: [ | ||
{ | ||
textMatches: [], | ||
user: { | ||
id: 'test-user', | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
fetchSearchResults.mockResolvedValue(mockData) | ||
const pageService = interpret(pageMachineWithMocks).onTransition((state) => { | ||
if (state.matches('waiting') && state.context.searchResults) { | ||
expect(state.context.searchResults).toMatchObject(mockData) | ||
done() | ||
} | ||
}) | ||
pageService.start() | ||
pageService.state.context.search.send(searchModel.events.updateInput('foo')) | ||
pageService.state.context.search.send(searchModel.events.submit()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { ActorRefFrom, spawn, assign, send } from 'xstate' | ||
import { createModel } from 'xstate/lib/model' | ||
import { searchMachine, searchModel } from './search' | ||
|
||
export type ErrorCode = 'RATE_LIMITED' | 'UNKNOWN_ERROR' | ||
|
||
export const pageModel = createModel( | ||
{ | ||
search: null as ActorRefFrom<typeof searchMachine>, | ||
results: null, | ||
pagination: null, | ||
query: null as string | null, | ||
page: 1, | ||
searchResults: null, | ||
errorCode: null as ErrorCode | null, | ||
}, | ||
{ | ||
events: { | ||
updateQuery: (value: string) => ({ value }), | ||
}, | ||
} | ||
) | ||
|
||
export const pageMachine = pageModel.createMachine( | ||
{ | ||
id: 'page', | ||
context: pageModel.initialContext, | ||
initial: 'waiting', | ||
entry: pageModel.assign({ | ||
search: () => spawn(searchMachine, 'search'), | ||
}), | ||
states: { | ||
waiting: { | ||
on: { | ||
updateQuery: { | ||
target: 'loading', | ||
actions: pageModel.assign({ | ||
query: (_, event) => event.value, | ||
}), | ||
}, | ||
}, | ||
}, | ||
loading: { | ||
invoke: { | ||
id: 'fetchSearchResults', | ||
src: 'fetchSearchResults', | ||
onDone: { | ||
target: 'waiting', | ||
actions: [ | ||
send(searchModel.events.loadingFinished(), { | ||
to: (context) => context.search, | ||
}), | ||
assign({ | ||
searchResults: (_, event) => event.data, | ||
}), | ||
], | ||
}, | ||
onError: { | ||
target: 'waiting', | ||
actions: assign({ | ||
errorCode: (_, event) => event.data, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
services: { | ||
fetchSearchResults: async () => { | ||
throw new Error('not implemented') | ||
}, | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { sendParent } from 'xstate' | ||
import { createModel } from 'xstate/lib/model' | ||
import { pageModel } from './page' | ||
|
||
export const searchModel = createModel( | ||
{ | ||
searchBoxInput: '', | ||
}, | ||
{ | ||
events: { | ||
updateInput: (value: string) => ({ value }), | ||
submit: () => ({}), | ||
loadingFinished: () => ({}), | ||
}, | ||
} | ||
) | ||
|
||
export const searchMachine = searchModel.createMachine({ | ||
id: 'search', | ||
context: searchModel.initialContext, | ||
initial: 'waiting', | ||
states: { | ||
waiting: { | ||
on: { | ||
updateInput: { | ||
actions: searchModel.assign({ | ||
searchBoxInput: (_, event) => event.value, | ||
}), | ||
}, | ||
submit: 'loading', | ||
}, | ||
}, | ||
loading: { | ||
entry: sendParent((context) => | ||
pageModel.events.updateQuery(context.searchBoxInput) | ||
), | ||
on: { | ||
loadingFinished: 'waiting', | ||
}, | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.build.json", | ||
"include": ["./src"], | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"outDir": "./dist" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters