-
Notifications
You must be signed in to change notification settings - Fork 0
/
Remold.test.js
145 lines (106 loc) · 3.61 KB
/
Remold.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React from 'react'
import Enzyme, { mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import { Remold, act, mold } from './Remold'
Enzyme.configure({ adapter: new Adapter() })
beforeEach(() => {
jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb());
});
afterEach(() => {
window.requestAnimationFrame.mockRestore();
});
const UserCard = ({ name, postfix = '' } = {}) => <p>{name}{postfix}</p>
class UserTitle extends React.Component { render = () => <h1>{this.props.name.toUpperCase()}</h1> }
test('Should have unique id', () => {
class Foo extends Remold {}
const first = new Foo(), second = new Foo()
expect(first.__REMOLD_ID__).not.toBe(second.__REMOLD_ID__)
expect(first.__REMOLD_ID__).toBe(first.__REMOLD_ID__)
})
test('Should save static properties', () => {
class Bar extends Remold { static value = 'bar' }
expect(Bar.value).toBe('bar')
})
test('Should save static methods', () => {
class Bar extends Remold {
static foo() { return new Bar() }
}
expect(Bar.foo()).toHaveProperty('__REMOLD_ID__')
})
describe('Remold subclass', () => {
class User extends Remold {
name = 'Remold'
age = 1
@act changeNameTo(aName) { this.name = aName }
@mold(UserCard) asCard(postfix) {
return { name: this.name, postfix }
}
@mold(UserTitle) asTitle() {
return { name: this.name }
}
}
let user, mountedCard, mountedTitle;
beforeEach(() => {
user = new User()
mountedCard = mount(user.asCard())
mountedTitle = mount(user.asTitle())
})
test('Should pass props into linked component', () => {
expect(mountedCard.text()).toBe('Remold')
expect(mountedTitle.text()).toBe('REMOLD')
})
test('Should pass additional props from arguments', () => {
const mountedCardWithPostfix = mount(user.asCard('Cool'))
expect(mountedCardWithPostfix.text()).toBe('RemoldCool')
})
test('Should reuse already created linked component', () => {
const firstCard = user.asCard(), secondCard = user.asCard()
expect(firstCard.type).toBe(secondCard.type)
})
test('Should rename linked component', () => {
expect(mountedCard.name()).toBe('MoldedUserCard')
expect(mountedTitle.name()).toBe('MoldedUserTitle')
})
test('Molds should be rerendered after changes', () => {
user.changeNameTo('New Remold')
expect(mountedCard.text()).toBe('New Remold')
expect(mountedTitle.text()).toBe('NEW REMOLD')
})
test('Molds should not be rerendered after unmount', () => {
let errorWasCalled = false
console.error = () => errorWasCalled = true
mountedCard.unmount()
user.changeNameTo('New Remold')
expect(errorWasCalled).toBeFalsy()
})
test('Mold with PureComponent should be rerendered only if needed', () => {
let renderCalledTimes = 0
class AgeComponent extends React.PureComponent {
render() {
renderCalledTimes += 1
return <p>{this.props.age}</p>
}
}
class AgeUser extends User {
@mold(AgeComponent) asAge() {
return { age: this.age }
}
}
const user = new AgeUser()
mount(user.asAge())
user.changeNameTo('New Name')
expect(renderCalledTimes).toBe(1)
})
test('Mold should have key', () => {
expect(user.asCard().key).toBe('UserCard-' + user.__REMOLD_ID__)
})
test('Act should be bound', () => {
function outer(aFunction, ...args) { aFunction(...args) }
outer(user.changeNameTo, 'FromOuter');
expect(user.name).toBe('FromOuter');
})
test('Mold should be bound', () => {
function outer(aFunction) { aFunction() }
expect(() => outer(user.asCard)).not.toThrowError();
})
})