UNPKG

2.25 kBJavaScriptView Raw
1import 'dom4';
2import React from 'react';
3import {shallow, mount} from 'enzyme';
4
5import Code from './code';
6
7describe('Code', () => {
8 const shallowCode = props => shallow(
9 <Code
10 code=""
11 {...props}
12 />,
13 {disableLifecycleMethods: true}
14 );
15 const mountCode = props => mount(
16 <Code
17 code=""
18 {...props}
19 />
20 );
21
22 it('should wrap children with pre', () => {
23 shallowCode().should.have.tagName('pre');
24 });
25
26 it('should use passed className', () => {
27 shallowCode({className: 'test-class'}).should.have.className('test-class');
28 });
29
30 it('should add passed language to class attribute', () => {
31 shallowCode({language: 'js'}).should.have.className('js');
32 });
33
34 it('should detect javascript/JSX', () => {
35 const wrapper = mountCode({
36 code: `
37 import React, {Component} from 'react';
38 import ChildComponent from './child-component';
39
40 const MyComponent = () => (
41 <div className="class">
42 <ChildComponent prop="value" />
43 </div>
44 );
45 `
46 });
47
48 // child tree is rendered with highlight.js, so it's unaccessible by enzyme
49 wrapper.getDOMNode().should.contain('.javascript');
50 wrapper.getDOMNode().should.contain('.xml');
51 });
52
53 it('should detect CSS', () => {
54 const wrapper = mountCode({
55 code: `
56 .className {
57 display: inline-block;
58 font-size: 18px;
59 }
60 `
61 });
62 wrapper.getDOMNode().should.contain('.css');
63 });
64
65 it('should detect HTML', () => {
66 const wrapper = mountCode({
67 code: `
68 <body>
69 <div id="app"></div>
70 </body>
71 `
72 });
73 wrapper.getDOMNode().should.contain('.xml');
74 });
75
76 it('should parse and highlight the code', () => {
77 const wrapper = mountCode({
78 code: '"foo"'
79 });
80 const token = wrapper.getDOMNode().query('.hljs-string');
81 token.textContent.should.equal('"foo"');
82 });
83
84 it('should parse and highlight the code after props update', () => {
85 const wrapper = mountCode({
86 code: '"foo"'
87 });
88 wrapper.setProps({
89 code: '"bar"'
90 });
91 const token = wrapper.getDOMNode().query('.hljs-string');
92 token.textContent.should.equal('"bar"');
93 });
94});