This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
/
MessageCard.test.js
304 lines (241 loc) · 8.27 KB
/
MessageCard.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import React from 'react'
import { mount, render } from 'enzyme'
import { MessageCard } from './MessageCard'
import {
TitleUI,
SubtitleUI,
BodyUI,
ActionUI,
ImageUI,
ImageContainerUI,
} from './MessageCard.css'
import { Animate } from '../index'
import { MessageCardButton as Button } from './MessageCard.Button'
describe('className', () => {
test('Has default className', () => {
const wrapper = render(<MessageCard />)
const el = wrapper.find('.c-MessageCard')
expect(el.length).toBeTruthy()
})
test('Can render custom className', () => {
const customClassName = 'blue'
const wrapper = render(<MessageCard className={customClassName} />)
const el = wrapper.find('.c-MessageCard')
expect(el.hasClass(customClassName)).toBeTruthy()
})
})
describe('Mobile', () => {
test('Should not have mobile styles by default', () => {
const wrapper = mount(<MessageCard />)
const el = wrapper.find('div.c-MessageCard')
expect(el.getDOMNode().classList.contains('is-mobile')).toBeFalsy()
})
test('Should have mobile styles if specified', () => {
const wrapper = mount(<MessageCard isMobile />)
const el = wrapper.find('div.c-MessageCard')
expect(el.getDOMNode().classList.contains('is-mobile')).toBeTruthy()
})
})
describe('Align', () => {
test('Has default alignment of right', () => {
const wrapper = mount(<MessageCard />)
const el = wrapper.find('div.c-MessageCard')
expect(el.getDOMNode().classList.contains('is-align-right')).toBeTruthy()
})
test('Can change alignment styles, if specified', () => {
const wrapper = mount(<MessageCard align="left" />)
const el = wrapper.find('div.c-MessageCard')
expect(el.getDOMNode().classList.contains('is-align-left')).toBeTruthy()
})
})
describe('Animation', () => {
test('Can customize animationSequence', () => {
const wrapper = mount(<MessageCard animationSequence="scale" />)
const o = wrapper.find(Animate)
expect(o.prop('sequence')).toBe('scale')
})
test('Can customize animationEasing', () => {
const wrapper = mount(<MessageCard animationEasing="linear" />)
const o = wrapper.find(Animate)
expect(o.prop('easing')).toBe('linear')
})
test('Can customize animationDuration', () => {
const wrapper = mount(<MessageCard animationDuration={123} />)
const o = wrapper.find(Animate)
expect(o.prop('duration')).toBe(123)
})
})
describe('Body', () => {
test('Does not render body if is not passed down as a prop', () => {
const wrapper = mount(<MessageCard />)
const o = wrapper.find(BodyUI)
expect(o.length).toBe(0)
})
test('Renders body if it is passed down as a prop', () => {
const wrapper = mount(<MessageCard body="Santa!" />)
const o = wrapper.find(BodyUI)
expect(o.length).toBe(1)
expect(o.html()).toContain('Santa!')
})
test('Renders html in body', () => {
const wrapper = mount(<MessageCard body="<span>Santa!</span>" />)
const o = wrapper.find(BodyUI)
expect(o.render().find('span').length).toBe(1)
})
test('Renders new line without html in body', () => {
const body = 'this is a new line\nwith another line'
const wrapper = mount(<MessageCard body={body} />)
const o = wrapper.find(BodyUI)
expect(o.render().find('br').length).toBe(1)
})
test('Accepts a custom onBodyClick callback', () => {
const body = 'some text with a <a href="#">link</a> in it'
const callback = jest.fn()
const wrapper = mount(<MessageCard body={body} onBodyClick={callback} />)
wrapper.simulate('click')
expect(callback).not.toHaveBeenCalled()
wrapper.find(BodyUI).simulate('click')
expect(callback).toHaveBeenCalled()
})
})
describe('Title', () => {
test('Does not render title if is not passed down as a prop', () => {
const wrapper = mount(<MessageCard />)
const o = wrapper.find(TitleUI)
expect(o.length).toBe(0)
})
test('Renders title if it is passed down as a prop', () => {
const wrapper = mount(<MessageCard title="Santa!" />)
const o = wrapper.find(TitleUI)
expect(o.length).toBe(1)
expect(o.html()).toContain('Santa!')
})
})
describe('Subtitle', () => {
test('Does not render subtitle if is not passed down as a prop', () => {
const wrapper = mount(<MessageCard />)
const o = wrapper.find(SubtitleUI)
expect(o.length).toBe(0)
})
test('Renders subtitle if it is passed down as a prop', () => {
const wrapper = mount(<MessageCard subtitle="Santa!" />)
const o = wrapper.find(SubtitleUI)
expect(o.length).toBe(1)
expect(o.html()).toContain('Santa!')
})
})
describe('image', () => {
test('Does not render image if is not passed down as a prop', () => {
const wrapper = mount(<MessageCard />)
const image = wrapper.find('img')
expect(image).toHaveLength(0)
})
test('Renders image if it is passed down as a prop', () => {
const wrapper = mount(
<MessageCard image={{ url: 'https://path.to/image.png' }} />
)
const image = wrapper.find('img')
expect(image).toHaveLength(1)
expect(image.prop('src')).toEqual('https://path.to/image.png')
})
test('Sets size of image when provided', () => {
const wrapper = mount(
<MessageCard
image={{
url: 'https://path.to/image.png',
width: '100',
height: '200',
}}
/>
)
const image = wrapper.find(ImageUI)
expect(image.prop('width')).toEqual('100px')
expect(image.prop('height')).toEqual('200px')
})
test('Scales size of image when larger than fits and width is bigger', () => {
const wrapper = mount(
<MessageCard
image={{
url: 'https://path.to/image.png',
width: '800',
height: '300',
}}
/>
)
const image = wrapper.find(ImageUI)
expect(image.prop('height')).toEqual('104.25px')
expect(image.prop('width')).toEqual('278px')
})
test('Scales size of image when larger than fits and height is bigger', () => {
const wrapper = mount(
<MessageCard
image={{
url: 'https://path.to/image.png',
width: '300',
height: '800',
}}
/>
)
const image = wrapper.find(ImageUI)
expect(image.prop('height')).toEqual('278px')
expect(image.prop('width')).toEqual('104.25px')
})
test('Sets default size of image when not provided', () => {
const wrapper = mount(
<MessageCard image={{ url: 'https://path.to/image.png' }} />
)
const image = wrapper.find(ImageUI)
expect(image.prop('width')).toEqual('100%')
expect(image.prop('height')).toEqual('auto')
})
test('Sets provided alt text', () => {
const wrapper = mount(
<MessageCard
image={{ url: 'https://path.to/image.png', altText: 'Alt text' }}
/>
)
const image = wrapper.find('img')
expect(image.prop('alt')).toEqual('Alt text')
})
test('Sets default alt text', () => {
const wrapper = mount(
<MessageCard image={{ url: 'https://path.to/image.png' }} />
)
const image = wrapper.find('img')
expect(image.prop('alt')).toEqual('Message image')
})
})
describe('Action', () => {
test('Does not render action if is not passed down as a prop', () => {
const wrapper = mount(<MessageCard />)
const o = wrapper.find(ActionUI)
expect(o.length).toBe(0)
})
test('Renders action if it is passed down as a prop', () => {
const action = () => <div>Click here</div>
const wrapper = mount(<MessageCard action={action} />)
const o = wrapper.find(ActionUI)
expect(o.length).toBe(1)
expect(o.html()).toContain('Click here')
})
test('Should remove the box shadow', () => {
const wrapper = mount(<MessageCard isWithBoxShadow={false} />)
const el = wrapper.find('div.c-MessageCard')
expect(el.getDOMNode().classList.contains('is-with-box-shadow')).toBeFalsy()
})
})
describe('Message Button Children', () => {
test('Can render children', () => {
const children = 'Hello world'
const wrapper = mount(<Button>{children}</Button>)
expect(wrapper.html()).toContain(children)
})
})
describe('Message Button onClick', () => {
test('Can accept custom onClick callback', () => {
const callback = jest.fn()
const wrapper = mount(<Button onClick={callback}>Click Me</Button>)
wrapper.simulate('click')
expect(callback).toHaveBeenCalled()
})
})