forked from mikaelvesavuori/figmagic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetFileContentAndPath.test.ts
More file actions
211 lines (178 loc) · 5.77 KB
/
getFileContentAndPath.test.ts
File metadata and controls
211 lines (178 loc) · 5.77 KB
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
import { getFileContentAndPath } from '../../../bin/frameworks/filesystem/getFileContentAndPath';
import {
getFileContentAndPathOperationToken,
getFileContentAndPathOperationComponent,
getFileContentAndPathOperationStyle,
getFileContentAndPathOperationCss,
getFileContentAndPathOperationStory,
getFileContentAndPathOperationDescription,
expectedEnum,
expectedStandard,
expectedTs,
expectedCss,
expectedScss
} from '../../../testdata/getFileContentAndPathOperation';
import {
ErrorGetFileContentAndPath,
ErrorGetFileContentAndPathMissingFields,
ErrorGetFileContentAndPathNoReturn
} from '../../../bin/frameworks/errors/errors';
describe('Failure cases', () => {
test('It should throw an error if running without arguments', () => {
// @ts-ignore
expect(() => getFileContentAndPath()).toThrowError(ErrorGetFileContentAndPath);
});
test('It should throw an error if missing any fields', () => {
// @ts-ignore
expect(() => getFileContentAndPath({})).toThrowError(ErrorGetFileContentAndPathMissingFields);
});
test('It should throw an error if it cannot resolve a valid type', () => {
const invalidContentType = {
type: 1,
file: 1,
path: 1,
name: 1,
format: 1,
element: 1
};
expect(() =>
// @ts-ignore
getFileContentAndPath(invalidContentType)
).toThrowError(ErrorGetFileContentAndPathNoReturn);
});
});
describe('Success cases', () => {
describe('Tokens', () => {
test('It should return valid data for MJS', () => {
expect(
// @ts-ignore
getFileContentAndPath(getFileContentAndPathOperationToken)
).toMatchObject({
fileContent: expectedStandard,
filePath: 'tokens/colors.mjs'
});
});
test('It should return valid data for TS', () => {
expect(
// @ts-ignore
getFileContentAndPath({ ...getFileContentAndPathOperationToken, format: 'ts' })
).toMatchObject({
fileContent: expectedTs,
filePath: 'tokens/colors.ts'
});
});
test('It should return valid data for enum', () => {
const data = {
...getFileContentAndPathOperationToken,
metadata: {
dataType: 'enum'
}
};
expect(
// @ts-ignore
getFileContentAndPath(data)
).toMatchObject({
fileContent: expectedEnum,
filePath: 'tokens/colors'
});
});
test('It should return valid data for CSS variables', () => {
expect(
// @ts-ignore
getFileContentAndPath({ ...getFileContentAndPathOperationToken, format: 'css' })
).toMatchObject({
fileContent: expectedCss,
filePath: 'tokens/colors.css'
});
});
test('It should return valid data for SCSS variables', () => {
expect(
// @ts-ignore
getFileContentAndPath({ ...getFileContentAndPathOperationToken, format: 'scss' })
).toMatchObject({
fileContent: expectedScss,
filePath: 'tokens/_colors.scss'
});
});
});
describe('Components', () => {
// TODO: Test fails inexplicably on Windows?
test('It should return valid data for components', () => {
const fileContent = `import React from 'react';
import PropTypes from 'prop-types';
import colorsStyled from './colorsStyled';
const colors = (props) => <colorsStyled {...props}>{props.children ? props.children : \"text\"}</colorsStyled>;
colors.propTypes = {};
export default colors;`;
expect(
// @ts-ignore
getFileContentAndPath(getFileContentAndPathOperationComponent)
).toMatchObject({
fileContent: fileContent,
filePath: 'tokens/colors.mjs'
});
});
});
describe('Style', () => {
test('It should return valid data for style', () => {
const fileContent = `import styled from 'styled-components';
import ColorsCss from './ColorsCss';
// Extend the below as needed
const ColorsStyled = styled.div\`
\${ColorsCss};
\`;
export default ColorsStyled;`;
expect(
// @ts-ignore
getFileContentAndPath(getFileContentAndPathOperationStyle)
).toMatchObject({
fileContent: fileContent,
filePath: 'tokens/colorsStyled.mjs'
});
});
});
describe('CSS', () => {
test('It should return valid data for CSS', () => {
const fileContent = `// THIS FILE IS AUTO-GENERATED BY FIGMAGIC. DO NOT MAKE EDITS IN THIS FILE! CHANGES WILL GET OVER-WRITTEN BY ANY FURTHER PROCESSING.
\n
const colorsCss = \`[object Object]\`;
export default colorsCss;`;
expect(
// @ts-ignore
getFileContentAndPath(getFileContentAndPathOperationCss)
).toMatchObject({
fileContent: fileContent,
filePath: 'tokens/colorsCss.mjs'
});
});
});
describe('Story', () => {
test('It should return valid data for Storybook', () => {
const fileContent = `import React from 'react';
import colors from './colors';
import notes from './colors.description.md';
export default { title: 'colors', parameters: { notes } };
export const colorsRegular = () => <colors>text</colors>;`;
expect(
// @ts-ignore
getFileContentAndPath(getFileContentAndPathOperationStory)
).toMatchObject({
fileContent: fileContent,
filePath: 'tokens/colors.stories.js'
});
});
});
describe('Description', () => {
test('It should return valid data for description', () => {
const fileContent = `<!--THIS FILE IS AUTO-GENERATED BY FIGMAGIC. DO NOT MAKE EDITS IN THIS FILE! CHANGES WILL GET OVER-WRITTEN BY ANY FURTHER PROCESSING.-->
[object Object]`;
expect(
// @ts-ignore
getFileContentAndPath(getFileContentAndPathOperationDescription)
).toMatchObject({
fileContent: fileContent,
filePath: 'tokens/colors.description.mjs'
});
});
});
});