UNPKG

5.31 kBJavaScriptView Raw
1const test = require('ava')
2const path = require('path')
3const {
4 testGenerator,
5 generateContent,
6 getFileName,
7 testCheckArcs,
8 testCheckBars,
9 testCheckSvg,
10 testCheckAxes,
11 saveFile,
12} = require('../lib/test-generator')
13const { STORYBOOK_PORT } = require('../lib/config')
14const fixtures = require('../example-src/components/simple-component.fixture.js').default
15
16const IMPORT_INDEX = 0
17const DESCRIBE_INDEX = 1
18
19const pathToSrc = `${__dirname}/../example-src`
20const pathToTestIndex = `${__dirname}/spdt`
21const file = '../../example-src/components/simple-component.story.js'
22const componentName = 'SimpleComponent'
23const fixtureName = Object.keys(fixtures)[0]
24
25const setup = () => {
26 const fileName = getFileName({
27 pathToTestIndex,
28 file,
29 })
30 const content = generateContent({
31 fixtures,
32 componentName,
33 })
34 return {
35 fileName,
36 content,
37 pathToSrc,
38 pathToTestIndex,
39 componentName,
40 fixtureName,
41 file,
42 }
43}
44
45test(`getFileName: check relative path generation`, (t) => {
46 const { fileName, file: fl, pathToTestIndex: ptti } = setup()
47 const expected = [ptti, '/', fl.replace('.story.', '.generated.spdt.')].join('')
48 t.is(fileName, expected)
49})
50
51test(`generateContent: check generated imports`, (t) => {
52 const { content } = setup()
53 const expected = `const TIMEOUT = 5000
54`
55 t.is(content[IMPORT_INDEX], expected)
56})
57
58test(`generateContent: check generated describe title`, (t) => {
59 const { content, componentName: cn, fixtureName: fn } = setup()
60 const expected = `${cn} - fixture ${fn}`
61 t.true(content[DESCRIBE_INDEX].includes(expected))
62})
63
64test(`generateContent: check generated describe page.goto url`, (t) => {
65 const { content, componentName: cn, fixtureName: fn } = setup()
66 const expected = `http://localhost:${STORYBOOK_PORT}/?selectedKind=${cn}&selectedStory=${fn}`
67 t.true(content[DESCRIBE_INDEX].includes(expected))
68})
69
70test('testGenerator', (t) => {
71 testGenerator({
72 fixtures,
73 file,
74 componentName,
75 pathToTestIndex,
76 })
77 t.pass()
78})
79
80test('testCheckArcs: when declaration not set should return empty string', (t) => {
81 const fixture = {}
82 const actual = testCheckArcs(fixture)
83 const expected = null
84 t.is(actual, expected)
85})
86
87test('testCheckArcs: when declaration is set should return generated it test', (t) => {
88 const fixture = {
89 props: {
90 data: [1, 2, 3],
91 },
92 spdt: { checkArcs: true },
93 }
94 const actual = testCheckArcs(fixture)
95 const expectedItTitle = `it('should have ${fixture.props.data.length} arcs according to fixture data'`
96 const expectedSelector = `const arcs = await iFrame.$$('path.arc')`
97 t.true(actual.includes(expectedItTitle))
98 t.true(actual.includes(expectedSelector))
99})
100
101test('testCheckAxes: when declaration not set should return empty string', (t) => {
102 const fixture = {}
103 const actual = testCheckAxes(fixture)
104 const expected = null
105 t.is(actual, expected)
106})
107
108test('testCheckAxes: when declaration is set should return generated it test', (t) => {
109 const checkAxes = 2
110 const fixture = {
111 props: {
112 data: [1, 2, 3],
113 },
114 spdt: { checkAxes },
115 }
116 const actual = testCheckAxes(fixture)
117 const expectedItTitle = `it('should have ${checkAxes} axes'`
118 const expectedSelector = `const axes = await iFrame.$$('g.axis')`
119 const expectedValue = `const expected = ${checkAxes}`
120 t.true(actual.includes(expectedItTitle))
121 t.true(actual.includes(expectedSelector))
122 t.true(actual.includes(expectedValue))
123})
124
125test('testCheckBars: when declaration not set should return empty string', (t) => {
126 const fixture = {}
127 const actual = testCheckBars(fixture)
128 const expected = null
129 t.is(actual, expected)
130})
131
132test('testCheckBars: when declaration is set should return generated it test', (t) => {
133 const fixture = {
134 props: {
135 data: [1, 2, 3],
136 },
137 spdt: { checkBars: true },
138 }
139 const actual = testCheckBars(fixture)
140 const expectedItTitle = `it('should have ${fixture.props.data.length} bars according to fixture data'`
141 const expectedSelector = `const bars = await iFrame.$$('rect.bar')`
142 t.true(actual.includes(expectedItTitle))
143 t.true(actual.includes(expectedSelector))
144})
145
146test('testCheckSvg: when declaration not set should return empty string', (t) => {
147 const fixture = {}
148 const actual = testCheckSvg(fixture)
149 const expected = null
150 t.is(actual, expected)
151})
152
153test('testCheckSvg: when declaration is set should return generated it test', (t) => {
154 const fixture = {
155 props: {
156 data: [1, 2, 3],
157 },
158 spdt: { checkSvg: true },
159 }
160 const actual = testCheckSvg(fixture)
161 const expectedItTitle = `it('should load component as <svg>'`
162 const expectedSelector = `const component = await iFrame.$('svg')`
163 t.true(actual.includes(expectedItTitle))
164 t.true(actual.includes(expectedSelector))
165})
166
167test('saveFile: when path is incorrect should put error to debug logger', (t) => {
168 const incorrectFile = path.resolve(__dirname, 'non-existed-dir', 'test-file-to.save')
169 const content = 'test content'
170 saveFile(incorrectFile, content)
171 t.pass()
172})
173test('saveFile: when path is correct should put saved file to debug logger', (t) => {
174 const incorrectFile = path.resolve(__dirname, 'e2e', 'test-file-to.save')
175 const content = 'test content'
176 saveFile(incorrectFile, content)
177 t.pass()
178})