Skip to content

Commit 2465ea3

Browse files
committed
add tests for creating and resetting fields in the subprocess #1
1 parent 7af290b commit 2465ea3

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.idea/
2+
.nvmrc
23
example
34
src
4-
__test__
55
tsconfig.json
66
ipos-*.tgz

src/__test__/createFields.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import IPOS from '../main'
33
import createFieldsTest from './runCreateFieldsTest'
44
import createConnectedInstances from './createConnectedInstances'
55

6-
describe('Updating fields between processes (transferring newly created fields)', () =>
6+
describe('Creating fields in the main process', () =>
77
createFieldsTest(
8-
async (setValue: (main_ipos: IPOS) => void, probeValue: (sub_ipos: IPOS) => void) => {
8+
async (setValue: (ipos_for_setting: IPOS) => void, probeValue: (ipos_for_probing: IPOS) => void) => {
99
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
1010

1111
// @ts-ignore Variable 'main_ipos' is used before being assigned.
@@ -25,3 +25,26 @@ describe('Updating fields between processes (transferring newly created fields)'
2525
: `Create a ${key}`
2626
)
2727
)
28+
29+
describe('Creating fields in the subprocess', () =>
30+
createFieldsTest(
31+
async (setValue: (ipos_for_setting: IPOS) => void, probeValue: (ipos_for_probing: IPOS) => void) => {
32+
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
33+
34+
// @ts-ignore Variable 'main_ipos' is used before being assigned.
35+
setValue(sub_ipos)
36+
37+
// make sure value is transmitted
38+
await new Promise(res => setTimeout(res, 1))
39+
40+
// @ts-ignore Variable 'sub_ipos' is used before being assigned.
41+
probeValue(main_ipos)
42+
43+
sub_process.destroy()
44+
},
45+
(key) =>
46+
['a', 'e', 'i', 'o', 'u'].some(vowel => key.startsWith(vowel))
47+
? `Create an ${key}`
48+
: `Create a ${key}`
49+
)
50+
)

src/__test__/resetFields.test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import IPOS from '../main'
33
import createFieldsTest from './runCreateFieldsTest'
44
import createConnectedInstances from './createConnectedInstances'
55

6-
describe('Updating fields between processes (transferring newly created fields)', () =>
6+
describe('Resetting fields in the main process', () =>
77
createFieldsTest(
8-
async (setValue: (main_ipos: IPOS, reset: boolean) => void, probeValue: (sub_ipos: IPOS) => void) => {
8+
async (setValue: (ipos_for_setting: IPOS, reset: boolean) => void, probeValue: (ipos_for_probing: IPOS) => void) => {
99
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
1010

1111
main_ipos.create('myField', 'myValue')
@@ -18,3 +18,19 @@ describe('Updating fields between processes (transferring newly created fields)'
1818
(key) => `Reset ${key}`
1919
)
2020
)
21+
22+
describe('Resetting fields in the sub process', () =>
23+
createFieldsTest(
24+
async (setValue: (ipos_for_setting: IPOS, reset: boolean) => void, probeValue: (ipos_for_probing: IPOS) => void) => {
25+
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
26+
27+
main_ipos.create('myField', 'myValue')
28+
setValue(sub_ipos, true)
29+
30+
probeValue(main_ipos)
31+
32+
sub_process.destroy()
33+
},
34+
(key) => `Reset ${key}`
35+
)
36+
)

src/__test__/runCreateFieldsTest.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import IPOS from '../main'
33
import {examples} from './testData'
44

55
export default function createFieldsTest(
6-
initWith: (setValue: (main_ipos: IPOS, reset?: boolean) => void, probeValue: (sub_ipos: IPOS) => void) => void,
6+
initWith: (setValue: (ipos_for_setting: IPOS, reset?: boolean) => void, probeValue: (ipos_for_probing: IPOS) => void) => void,
77
createLabel: (key: string) => string
88
) {
99
function customizer(a: any, b: any) {
@@ -18,17 +18,17 @@ export default function createFieldsTest(
1818
it(createLabel(exampleKey), async () => {
1919
const value: unknown = examples[exampleKey]
2020
await initWith(
21-
(main_ipos, reset = false) => {
21+
(ipos_for_setting, reset = false) => {
2222
if (!reset) {
23-
main_ipos.create('myField', value)
23+
ipos_for_setting.create('myField', value)
2424
} else {
25-
main_ipos.myField = value
25+
ipos_for_setting.myField = value
2626
}
2727
},
28-
(sub_ipos) => {
28+
(ipos_for_probing) => {
2929
// lodash equal to compare maps and sets
30-
expect(_.isEqualWith(sub_ipos.myField, value, customizer)).toEqual(true)
31-
expect(_.isEqualWith(sub_ipos.get('myField'), value, customizer)).toEqual(true)
30+
expect(_.isEqualWith(ipos_for_probing.myField, value, customizer)).toEqual(true)
31+
expect(_.isEqualWith(ipos_for_probing.get('myField'), value, customizer)).toEqual(true)
3232
}
3333
)
3434
})

src/__test__/synchronize.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import subProcessIPCLoopback from './subProcessIPCLoopback'
44
import {withoutProcessSendSync} from './withoutProcessSendSync'
55
import createFieldsTest from './runCreateFieldsTest'
66

7-
describe('Synchronisation of fields between processes (transferring existing fields)', () =>
7+
describe('Synchronising fields', () =>
88
createFieldsTest(
9-
async (setValue: (main_ipos: IPOS) => void, probeValue: (sub_ipos: IPOS) => void) => {
9+
async (setValue: (ipos_for_setting: IPOS) => void, probeValue: (ipos_for_probing: IPOS) => void) => {
1010
const sub_process = new subProcessIPCLoopback()
1111
let main_ipos: IPOS, sub_ipos: IPOS
1212
await withoutProcessSendSync(() => {

0 commit comments

Comments
 (0)