Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Add tests for effector/fork #7

Merged
merged 3 commits into from
Mar 26, 2020
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
File renamed without changes.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"scripts": {
"dev": "ts-node src/index.ts",
"test": "jest --config jestconfig.json",
"test": "jest",
"build": "rm -rf pkg/ && pika build",
"format": "prettier --write \"src/**/*.ts\"",
"lint": "tslint -p tsconfig.json && yarn spell",
Expand Down Expand Up @@ -70,7 +70,7 @@
"@pika/plugin-ts-standard-pkg": "^0.9.2",
"@size-limit/preset-small-lib": "^4.3.1",
"@types/jest": "^25.1.4",
"effector": "^20.12.2",
"effector": "^20.13.4",
"jest": "^25.1.0",
"pika-plugin-package.json": "^1.0.2",
"prettier": "^1.19.1",
Expand Down
110 changes: 110 additions & 0 deletions src/fork.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { createDomain, forward } from 'effector'
import { fork, serialize, allSettled } from 'effector/fork'
import { createReEffectFactory } from './createReEffect'

test('createReEffect resolves in fork by default', async () => {
const createReEffect = createReEffectFactory()

const app = createDomain()
const start = app.createEvent()
const $store = app.createStore(0, { name: '$store', sid: '$store' })
const reeffect = createReEffect({
async handler() {
return 5
},
})

$store.on(reeffect.done, (state, { result }) => state + result)

forward({
from: start,
to: reeffect,
})

const scope = fork(app)

await allSettled(start, {
scope,
params: undefined,
})

expect(serialize(scope)).toMatchInlineSnapshot(`
Object {
"$store": 5,
}
`)
})

test('createReEffect do not affect other forks', async () => {
const createReEffect = createReEffectFactory()

const app = createDomain()
const start = app.createEvent<number>()
const $store = app.createStore(0, { sid: '$store' })
const reeffect = createReEffect({
async handler(param: number) {
return 5 * param
},
})

$store.on(reeffect.done, (state, { result }) => state + result)

forward({
from: start,
to: reeffect,
})

const scopeAlice = fork(app)
const scopeBob = fork(app)

await allSettled(start, {
scope: scopeAlice,
params: 10,
})

await allSettled(start, {
scope: scopeBob,
params: 1000,
})

expect(serialize(scopeAlice)).toMatchInlineSnapshot(`
Object {
"$store": 50,
}
`)

expect(serialize(scopeBob)).toMatchInlineSnapshot(`
Object {
"$store": 5000,
}
`)
})

test('createReEffect in fork do not affect domain', async () => {
const createReEffect = createReEffectFactory()

const app = createDomain()
const start = app.createEvent()
const $store = app.createStore(0, { name: '$store', sid: '$store' })
const reeffect = createReEffect({
async handler() {
return 5
},
})

$store.on(reeffect.done, (state, { result }) => state + result)

forward({
from: start,
to: reeffect,
})

const scope = fork(app)

await allSettled(start, {
scope,
params: undefined,
})

expect($store.getState()).toMatchInlineSnapshot(`0`)
})
Loading