-
Notifications
You must be signed in to change notification settings - Fork 2
/
runAndMonitor_path.spec.ts
91 lines (86 loc) · 2.39 KB
/
runAndMonitor_path.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* eslint-env jest */
import * as R from 'ramda';
import _ from 'lodash';
import SubX from '../src/index';
import {HandlerEvent, TransactionEvent} from '../src/types';
describe('runAndMonitor path', () => {
test('root is subx', () => {
const p = SubX.create({
number: 1,
});
const stream$ = SubX.runAndMonitor(p, () => p.number).stream$;
const events: HandlerEvent[] = [];
stream$.subscribe(e => events.push(e));
p.number = 2;
expect(R.map(R.dissoc('id'), events)).toEqual([
{type: 'SET', path: ['number']},
]);
});
test('root is not subx', () => {
// now root must be subx
const p = SubX.create({
number: 1,
});
const stream$ = SubX.runAndMonitor(
SubX.create({child: p}),
() => p.number
).stream$;
const events: HandlerEvent[] = [];
stream$.subscribe(e => events.push(e));
p.number = 2;
expect(R.map(R.dissoc('id'), events)).toEqual([
{type: 'SET', path: ['child', 'number']},
]);
});
test('TodoMVC save', () => {
const store = SubX.create({
todos: [
{
title: '111',
completed: false,
},
],
visibility: 'all',
});
const stream$ = SubX.runAndMonitor(store, () =>
JSON.stringify(store.todos)
).stream$;
const events: HandlerEvent[] = [];
stream$.subscribe(e => events.push(e));
store.todos.push({title: '222', completed: false});
expect(events.length).toBe(1);
let event = events[0] as TransactionEvent;
event = _.omit(event, 'id') as TransactionEvent;
event.events = _.map(event.events, e => _.omit(e, 'id') as HandlerEvent);
expect(event).toEqual({
events: [
{path: ['1'], type: 'SET'},
{path: ['length'], type: 'SET'},
],
name: 'push',
path: ['todos'],
type: 'TRANSACTION',
});
});
test('toJSON events', () => {
const store = SubX.create({
todos: [
{
title: '111',
completed: false,
},
],
visibility: 'all',
});
const gets = [];
const hass = [];
const keyss = [];
store.get$.subscribe(e => gets.push(e));
store.has$.subscribe(e => hass.push(e));
store.keys$.subscribe(e => keyss.push(e));
expect(JSON.stringify(store.todos)).toBeDefined();
expect(gets.length).toBeGreaterThan(0);
expect(hass.length).toBe(0);
expect(keyss.length).toBeGreaterThan(0);
});
});