Skip to content

Commit

Permalink
feat(overmind): completely rewrite statemachine API even once more
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Oct 7, 2020
1 parent 629810f commit 7ff9396
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 270 deletions.
224 changes: 23 additions & 201 deletions packages/node_modules/overmind/src/statemachine.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { PROXY_TREE } from 'proxy-state-tree'

import { IAction, createOvermind, createOvermindMock } from './'
import { Statemachine, statemachine } from './statemachine'
import { statemachine } from './statemachine'

describe('Statemachine', () => {

test('should set initial state', () => {

type States = {
type States = {
current: 'FOO'
} | {
current: 'BAR'
Expand All @@ -31,24 +29,24 @@ describe('Statemachine', () => {
})



test('should transition state', () => {
type States = {
current: 'FOO'
} | {
current: 'BAR'
}

const state = statemachine<States>({
FOO: ['BAR'],
BAR: ['FOO']
type Events = {
type: 'TOGGLE',
}

const state = statemachine<States, Events>({
TOGGLE: (state) => state.current === 'FOO' ? 'BAR' : 'FOO'
}).create({
current: 'FOO'
})
const transition: Action = ({ state }) => {
state.transition({
current: 'BAR'
})
state.transition('TOGGLE')
}

const config = {
Expand All @@ -67,166 +65,26 @@ describe('Statemachine', () => {
expect(overmind.state.current).toBe('BAR')
})

test('should ignore transition to invalid state', () => {

type States = {
current: 'FOO'
} | {
current: 'BAR'
}

const state = statemachine<States>({
FOO: [],
BAR: ['FOO']
}).create({
current: 'FOO'
})
const transition: Action = ({ state }) => {
state.transition({
current: 'BAR'
})
}

const config = {
state,
actions: {
transition
}
}

interface Action extends IAction<typeof config, void, void> {}

const overmind = createOvermindMock(config)
overmind.actions.transition()
expect(overmind.state.current).toBe('FOO')
})

test('should allow dynamic transitions to prevent transition', () => {
type States = {
current: 'FOO'
} | {
current: 'BAR'
}
test('should ignore transition when no state returned', () => {

const state = statemachine<States>({
FOO: ['BAR'],
BAR: () => false
}).create({
current: 'FOO'
})
const transition: Action = ({ state }) => {
state.transition({
current: 'BAR'
})
}

const config = {
state,
actions: {
transition
}
}

interface Action extends IAction<typeof config, void, void> {}

const overmind = createOvermindMock(config)


overmind.actions.transition()
expect(overmind.state.current).toBe('FOO')
})

test('should allow dynamic transitions', () => {
type States = {
current: 'FOO'
} | {
current: 'BAR'
}

const state = statemachine<States>({
FOO: ['BAR'],
BAR: () => ['FOO']
}).create({
current: 'FOO'
})
const transition: Action = ({ state }) => {
state.transition({
current: 'BAR'
})
expect(overmind.state.current).toBe('BAR')
state.transition({
current: 'FOO'
})
}

const config = {
state,
actions: {
transition
}
}

interface Action extends IAction<typeof config, void, void> {}

const overmind = createOvermindMock(config)


overmind.actions.transition()
expect(overmind.state.current).toBe('FOO')
})

test('should allow transition from dynamic transition', () => {
type States = {
current: 'FOO'
} | {
current: 'BAR'
}

const state = statemachine<States>({
FOO: ['BAR'],
BAR: () => ({ current: 'FOO' })
}).create({
current: 'FOO'
})
const transition: Action = ({ state }) => {
state.transition({
current: 'BAR'
})
}

const config = {
state,
actions: {
transition
}
}

interface Action extends IAction<typeof config, void, void> {}

const overmind = createOvermindMock(config)


overmind.actions.transition()
expect(overmind.state.current).toBe('FOO')
})

test('should allow back transition', () => {
type States = {
current: 'FOO'
} | {
current: 'BAR'
}
type Events = {
type: 'TOGGLE',
}

const state = statemachine<States>({
FOO: ['BAR'],
BAR: () => true
const state = statemachine<States, Events>({
TOGGLE: () => {}
}).create({
current: 'FOO'
})
const transition: Action = ({ state }) => {
state.transition({
current: 'BAR'
})
state.transition('TOGGLE')
}

const config = {
Expand All @@ -239,48 +97,11 @@ describe('Statemachine', () => {
interface Action extends IAction<typeof config, void, void> {}

const overmind = createOvermindMock(config)


overmind.actions.transition()
expect(overmind.state.current).toBe('FOO')
})

test('should allow using callback for transition', () => {
type States = {
current: 'FOO'
} | {
current: 'BAR'
bar: string
}

const state = statemachine<States>({
FOO: ['BAR'],
BAR: []
}).create({
current: 'FOO'
})
const transition: Action = ({ state }) => {
state.transition('BAR', (state) => {
state.bar = 'bar2'
})
}

const config = {
state,
actions: {
transition
}
}

interface Action extends IAction<typeof config, void, void> {}

const overmind = createOvermindMock(config)


overmind.actions.transition()
expect(overmind.state.current).toBe('BAR')
expect(overmind.state.matches('BAR')?.bar).toBe('bar2')
})

test('should flush changes to transitions', () => {
expect.assertions(1)
Expand All @@ -291,17 +112,18 @@ describe('Statemachine', () => {
current: 'BAR'
}

const state = statemachine<States>({
FOO: ['BAR'],
BAR: ['FOO']
type Events = {
type: 'TOGGLE',
}

const state = statemachine<States, Events>({
TOGGLE: () => 'BAR'
}).create({
current: 'FOO'
})

const transition: Action = ({ state }) => {
state.transition({
current: 'BAR'
})
state.transition('TOGGLE')
}

const config = {
Expand Down
Loading

0 comments on commit 7ff9396

Please sign in to comment.