|
1 | 1 | import React from 'react' |
2 | 2 | import { shallow, mount, configure } from 'enzyme' |
3 | 3 | import Adapter from 'enzyme-adapter-react-16' |
4 | | -import Cui from './ReactCui' |
| 4 | +import Cui, { EMBED_URL } from './ReactCui' |
5 | 5 |
|
6 | 6 | configure({ adapter: new Adapter() }) |
| 7 | +describe('<Cui />', () => { |
7 | 8 |
|
8 | | -test('Component have required UID parameter', () => { |
9 | | - const uid = 'abc123' |
10 | | - const Component = mount(<Cui uid={uid} />) |
11 | | - expect(Component.props().uid).toBe(uid) |
12 | | -}) |
| 9 | + test('Component have required UID parameter passed to render', () => { |
| 10 | + const uid = 'abc123' |
| 11 | + const Component = mount(<Cui uid={uid} />) |
| 12 | + expect(Component.render().attr('data-cui-uid')).toBe(uid) |
| 13 | + }) |
| 14 | + |
| 15 | + test('Component have optional parameters being passed to the DOM element', () => { |
| 16 | + const uid = 'abc123' |
| 17 | + const height = 500 |
| 18 | + const avatar = 'https://avatar.com/profile.jpg' |
| 19 | + const theme = '#333' |
| 20 | + const Component = mount(<Cui uid={uid} height={height} avatar={avatar} theme={theme} />) |
| 21 | + const ComponentDOM = Component.render() |
| 22 | + |
| 23 | + expect(ComponentDOM.attr('data-cui-height')).toBe(height.toString()) |
| 24 | + expect(ComponentDOM.attr('data-cui-avatar')).toBe(avatar) |
| 25 | + expect(ComponentDOM.attr('data-cui-theme')).toBe(theme) |
| 26 | + }) |
| 27 | + |
| 28 | + test('Component should have the className `cui-embed` so the scripts loads the CUI inside', () => { |
| 29 | + const uid = 'abc123' |
| 30 | + const Component = shallow(<Cui uid={uid} />) |
| 31 | + |
| 32 | + expect(Component.find('.cui-embed').length).toBe(1) |
| 33 | + }) |
| 34 | + |
| 35 | + test('Component should inject ONLY ONE script tag with the embed url', () => { |
| 36 | + |
| 37 | + const Component = mount(<Cui uid={'123'} />) |
| 38 | + const scriptTags = document.getElementsByTagName('head')[0].childNodes |
| 39 | + let embedScriptTags = 0 |
| 40 | + scriptTags.forEach(script => { |
| 41 | + if (script.src === EMBED_URL) embedScriptTags++ |
| 42 | + }) |
| 43 | + |
| 44 | + expect(embedScriptTags).toBe(1) |
13 | 45 |
|
14 | | -test('Component should have the className `cui-embed`', () => { |
15 | | - const uid = 'abc123' |
16 | | - const Component = shallow(<Cui uid={uid} />) |
| 46 | + }) |
17 | 47 |
|
18 | | - expect(Component.is('.cui-embed')).toBe(true) |
19 | 48 | }) |
0 commit comments