-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtiny.test.js
81 lines (78 loc) · 2.55 KB
/
tiny.test.js
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
import { create, update, getLocalChanges } from './tiny.js'
describe('tiny create', () => {
it('should create', () => {
const userId = '0x123'
const tiny = create('Hello World', userId)
expect(tiny).toEqual({
value: 'Hello World',
version: 0,
createdBy: userId,
lastUpdatedBy: userId,
})
})
})
describe('tiny update', () => {
it('should update when the version number is higher', () => {
const userId = '0x123'
const tiny = create('Hello world', userId)
const updated = update(tiny, 'The world says hello back', 1, userId)
expect(updated).toEqual({
value: 'The world says hello back',
version: 1,
createdBy: tiny.createdBy,
lastUpdatedBy: userId,
})
})
it('should update when the version number is equal and the userId is higher', () => {
const userId = '0x123'
const tiny = create('Hello world', userId)
const userId2 = '0x456'
const updated = update(tiny, 'The world says hello back', 0, userId2)
expect(updated).toEqual({
value: 'The world says hello back',
version: 0,
createdBy: tiny.createdBy,
lastUpdatedBy: userId2,
})
})
it('should not update when the version number is equal and the userId is lower', () => {
const userId = '0x123'
const tiny = create('Hello world', userId)
const updated = update(tiny, 'The world says hello back', 0, userId)
expect(updated).toEqual(null)
})
it('should not update when the version number is lower', () => {
const userId = '0x123'
const tiny = create('Hello world', userId, 1)
const updated = update(tiny, 'The world says hello back', 0, userId)
expect(updated).toEqual(null)
})
it('should not update when the version number is equal and the userId is equal', () => {
const userId = '0x123'
const tiny = create('Hello world', userId)
const updated = update(tiny, 'The world says hello back', 0, userId)
expect(updated).toEqual(null)
})
})
describe('tiny getLocalChanges', () => {
it('should get no local changes', () => {
const userId = '0x123'
const tiny = create('Hello world', userId)
const localChanges = getLocalChanges(tiny, 'Hello world', userId)
expect(localChanges).toEqual(null)
})
it('should detect changes', () => {
const userId = '0x123'
const tiny = create('Hello world', userId)
const localChanges = getLocalChanges(
tiny,
'The world says hello back',
userId
)
expect(localChanges).toEqual({
value: 'The world says hello back',
version: 1,
userId,
})
})
})