test(code): add testcase

This commit is contained in:
unix
2020-04-17 16:05:44 +08:00
parent dbcde10b76
commit 59a0580bc5
2 changed files with 670 additions and 0 deletions

View File

@@ -0,0 +1,623 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Code should repspond to changed by width 1`] = `
initialize {
"0": Object {
"attribs": Object {
"class": " ",
},
"children": Array [
Object {
"attribs": Object {},
"children": Array [
Object {
"data": "code",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "code",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": [Circular],
"prev": null,
"type": "tag",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
],
"name": "pre",
"namespace": "http://www.w3.org/1999/xhtml",
"next": Object {
"attribs": Object {},
"children": Array [
Object {
"data": "
pre {
width: 50%;
max-width: 100%;
}
.dark {
color: white;
background: black;
}
.dark code {
color: white;
}
pre:before {
content: '$ ';
display: none;
font-weight: 500;
user-select: none;
}
",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "style",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": [Circular],
"root": Object {
"attribs": Object {},
"children": Array [
[Circular],
[Circular],
],
"name": "root",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": null,
"type": "root",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"type": "style",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"parent": null,
"prev": null,
"root": Object {
"attribs": Object {},
"children": Array [
[Circular],
Object {
"attribs": Object {},
"children": Array [
Object {
"data": "
pre {
width: 50%;
max-width: 100%;
}
.dark {
color: white;
background: black;
}
.dark code {
color: white;
}
pre:before {
content: '$ ';
display: none;
font-weight: 500;
user-select: none;
}
",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "style",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": [Circular],
"root": [Circular],
"type": "style",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
],
"name": "root",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": null,
"type": "root",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"type": "tag",
"x-attribsNamespace": Object {
"class": undefined,
},
"x-attribsPrefix": Object {
"class": undefined,
},
},
"1": Object {
"attribs": Object {},
"children": Array [
Object {
"data": "
pre {
width: 50%;
max-width: 100%;
}
.dark {
color: white;
background: black;
}
.dark code {
color: white;
}
pre:before {
content: '$ ';
display: none;
font-weight: 500;
user-select: none;
}
",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "style",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": Object {
"attribs": Object {
"class": " ",
},
"children": Array [
Object {
"attribs": Object {},
"children": Array [
Object {
"data": "code",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "code",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": [Circular],
"prev": null,
"type": "tag",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
],
"name": "pre",
"namespace": "http://www.w3.org/1999/xhtml",
"next": [Circular],
"parent": null,
"prev": null,
"root": Object {
"attribs": Object {},
"children": Array [
[Circular],
[Circular],
],
"name": "root",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": null,
"type": "root",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"type": "tag",
"x-attribsNamespace": Object {
"class": undefined,
},
"x-attribsPrefix": Object {
"class": undefined,
},
},
"root": Object {
"attribs": Object {},
"children": Array [
Object {
"attribs": Object {
"class": " ",
},
"children": Array [
Object {
"attribs": Object {},
"children": Array [
Object {
"data": "code",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "code",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": [Circular],
"prev": null,
"type": "tag",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
],
"name": "pre",
"namespace": "http://www.w3.org/1999/xhtml",
"next": [Circular],
"parent": null,
"prev": null,
"root": [Circular],
"type": "tag",
"x-attribsNamespace": Object {
"class": undefined,
},
"x-attribsPrefix": Object {
"class": undefined,
},
},
[Circular],
],
"name": "root",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": null,
"type": "root",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"type": "style",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"_root": [Circular],
"length": 2,
"options": Object {
"decodeEntities": true,
"normalizeWhitespace": false,
"withDomLvl1": true,
"xml": false,
},
}
`;
exports[`Code should support block mode 1`] = `
initialize {
"0": Object {
"attribs": Object {
"class": " ",
},
"children": Array [
Object {
"attribs": Object {},
"children": Array [
Object {
"data": "code",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "code",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": [Circular],
"prev": null,
"type": "tag",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
],
"name": "pre",
"namespace": "http://www.w3.org/1999/xhtml",
"next": Object {
"attribs": Object {},
"children": Array [
Object {
"data": "
pre {
width: initial;
max-width: 100%;
}
.dark {
color: white;
background: black;
}
.dark code {
color: white;
}
pre:before {
content: '$ ';
display: none;
font-weight: 500;
user-select: none;
}
",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "style",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": [Circular],
"root": Object {
"attribs": Object {},
"children": Array [
[Circular],
[Circular],
],
"name": "root",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": null,
"type": "root",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"type": "style",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"parent": null,
"prev": null,
"root": Object {
"attribs": Object {},
"children": Array [
[Circular],
Object {
"attribs": Object {},
"children": Array [
Object {
"data": "
pre {
width: initial;
max-width: 100%;
}
.dark {
color: white;
background: black;
}
.dark code {
color: white;
}
pre:before {
content: '$ ';
display: none;
font-weight: 500;
user-select: none;
}
",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "style",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": [Circular],
"root": [Circular],
"type": "style",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
],
"name": "root",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": null,
"type": "root",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"type": "tag",
"x-attribsNamespace": Object {
"class": undefined,
},
"x-attribsPrefix": Object {
"class": undefined,
},
},
"1": Object {
"attribs": Object {},
"children": Array [
Object {
"data": "
pre {
width: initial;
max-width: 100%;
}
.dark {
color: white;
background: black;
}
.dark code {
color: white;
}
pre:before {
content: '$ ';
display: none;
font-weight: 500;
user-select: none;
}
",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "style",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": Object {
"attribs": Object {
"class": " ",
},
"children": Array [
Object {
"attribs": Object {},
"children": Array [
Object {
"data": "code",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "code",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": [Circular],
"prev": null,
"type": "tag",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
],
"name": "pre",
"namespace": "http://www.w3.org/1999/xhtml",
"next": [Circular],
"parent": null,
"prev": null,
"root": Object {
"attribs": Object {},
"children": Array [
[Circular],
[Circular],
],
"name": "root",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": null,
"type": "root",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"type": "tag",
"x-attribsNamespace": Object {
"class": undefined,
},
"x-attribsPrefix": Object {
"class": undefined,
},
},
"root": Object {
"attribs": Object {},
"children": Array [
Object {
"attribs": Object {
"class": " ",
},
"children": Array [
Object {
"attribs": Object {},
"children": Array [
Object {
"data": "code",
"next": null,
"parent": [Circular],
"prev": null,
"type": "text",
},
],
"name": "code",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": [Circular],
"prev": null,
"type": "tag",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
],
"name": "pre",
"namespace": "http://www.w3.org/1999/xhtml",
"next": [Circular],
"parent": null,
"prev": null,
"root": [Circular],
"type": "tag",
"x-attribsNamespace": Object {
"class": undefined,
},
"x-attribsPrefix": Object {
"class": undefined,
},
},
[Circular],
],
"name": "root",
"namespace": "http://www.w3.org/1999/xhtml",
"next": null,
"parent": null,
"prev": null,
"type": "root",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"type": "style",
"x-attribsNamespace": Object {},
"x-attribsPrefix": Object {},
},
"_root": [Circular],
"length": 2,
"options": Object {
"decodeEntities": true,
"normalizeWhitespace": false,
"withDomLvl1": true,
"xml": false,
},
}
`;

View File

@@ -0,0 +1,47 @@
import React from 'react'
import { mount, render } from 'enzyme'
import { Code } from '../../index'
describe('Code', () => {
it('should render correctly', () => {
const wrapper = mount(<Code>code</Code>)
expect(() => wrapper.unmount()).not.toThrow()
})
it('should support block mode', () => {
const wrapper = render(<Code block>code</Code>)
expect(wrapper).toMatchSnapshot()
})
it('should repspond to changed by width', () => {
const wrapper = render(<Code block width="50%">code</Code>)
expect(wrapper).toMatchSnapshot()
})
it('should render pre element only in block mode', () => {
const wrapper = mount(<Code>code</Code>)
expect(wrapper.find('pre').length).toBe(0)
wrapper.setProps({ block: true })
expect(wrapper.find('pre').length).not.toBe(0)
})
it('should alert warning when use bash', () => {
let errorMessage = ''
const errorSpy = jest.spyOn(console, 'error')
.mockImplementation(msg => errorMessage = msg)
mount(<Code bash>code</Code>)
expect(errorMessage.toLowerCase()).toContain('deprecated')
errorSpy.mockRestore()
})
it('should alert warning when use darkBash', () => {
let errorMessage = ''
const errorSpy = jest.spyOn(console, 'error')
.mockImplementation(msg => errorMessage = msg)
mount(<Code darkBash>code</Code>)
expect(errorMessage.toLowerCase()).toContain('deprecated')
errorSpy.mockRestore()
})
})