-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathredux.test.js
189 lines (178 loc) · 5.56 KB
/
redux.test.js
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
import reducer, { addComponent, clearProject, createComponent, reorderComponent, setCurrentFile } from '../src/redux/canvasSlice';
describe('canvasSlice reducers', () => {
let initialState;
function addCompTest(state, action) {
state.components.splice(action.destination.index, 0, action.draggableId);
state.tags.splice(action.destination.index, 0, '\n\t\t\t' + state.codeList[action.draggableId]);
return state;
}
function createAction(end, id, start) {
return {
destination: {
index: end,
},
source: {
index: start,
},
draggableId: id,
name: id,
};
}
beforeEach(() => {
initialState = {
components: [],
code: '',
cssCode: `html {
box-sizing: border-box;
height: 100%;
}
body {
margin: 0;
padding-top: 20%;
overflow: hidden;
background-color: #272727;
font-family: "Helvetica Neue";
display: flex;
justify-content: center;
text-align: center;
height: 100%;
}
h1 {
color: white;
font-size: 3rem;
}
p {
color: white;
font-size: 1.5rem;
}
.default-spans {
color: #4338ca;
}`,
tags: [],
customComponents: [],
imports: ["import React from 'react';\n"],
codeList: {
Div: `<div className=''></div>`,
Paragraph: `<p className=''></p>`,
Anchor: `<a href='' className=''></a>`,
Image: `<img className=''></img>`,
'Unordered List': `<ul className=''></ul>`,
Form: `<form className=''></form>`,
Input: `<input className=''></input>`,
'Ordered List': `<ol className=''></ol>`,
Button: `<button className=''></button>`,
'List Item': `<li className=''></li>`,
Span: `<span className=''></span>`,
'Header 1': `<h1 className=''></h1>`,
'Header 2': `<h2 className=''></h2>`,
'Header 3': `<h3 className=''></h3>`,
'Line Break': `<br>`,
Table: `<table className=''></table>`,
THead: `<thead className=''></thead>`,
},
files: [
{
type: 'file',
name: 'App.js',
fileCode: '',
fileTags: [],
fileImports: [],
fileComponents: [],
},
{
type: 'file',
name: 'styles.css',
fileCode: '',
fileTags: [],
fileImports: [],
fileComponents: [],
},
],
currentFile: 'App.js',
};
});
// issue: test skipped until fixed
xdescribe('addComponent:', () => {
test('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(initialState);
});
test('should add to initial state', () => {
const action = createAction(0, 'Div');
expect(reducer(initialState, addComponent(action))).toEqual(addCompTest(initialState, action));
});
test('should add to end of array', () => {
const action = createAction(1, 'Button');
expect(reducer(initialState, addComponent(action))).toEqual(addCompTest(initialState, action));
});
test('should add to middle of array', () => {
const action = createAction(1, 'Anchor');
expect(reducer(initialState, addComponent(action))).toEqual(addCompTest(initialState, action));
});
});
describe('reorderComponent:', () => {
test('should swap 2 items', () => {
const action = createAction(1, 'Button', 2);
const comps = ['Div', 'Button', 'Anchor'];
const tags = comps.map((ele) => '\n\t\t\t' + initialState.codeList[ele]);
addCompTest(initialState, createAction(0, 'Div'));
addCompTest(initialState, createAction(1, 'Anchor'));
addCompTest(initialState, createAction(2, 'Button'));
expect(reducer(initialState, reorderComponent(action))).toEqual({ ...initialState, components: comps, tags: tags });
});
});
// issue: test skipped until fixed
xdescribe('clearProject:', () => {
test('should reset initial state', () => {
addCompTest(initialState, createAction(0, 'Div'));
const newState = {
...initialState,
components: [],
tags: [],
};
expect(reducer(initialState, clearProject())).toEqual(newState);
});
});
describe('createComponent:', () => {
test('should create new custom component', () => {
const state = {
...initialState,
tags: ['\n\t\t\t<TestFile />'],
customComponents: ['TestFile'],
components: ['TestFile'],
imports: ["import React from 'react';\n", "import TestFile from './TestFile.jsx';\n"],
files: [
{
type: 'file',
name: 'App.js',
fileCode: '',
fileTags: [],
fileImports: [],
fileComponents: [],
},
{
type: 'file',
name: 'styles.css',
fileCode: '',
fileTags: [],
fileImports: [],
fileComponents: [],
},
{
type: 'file',
name: 'TestFile.jsx',
fileCode: `import React from 'react';\n\nconst TestFile = () => {\n\treturn (\n\t\t<div>\n\t\t</div>\n\t)\n}\nexport default TestFile;`,
fileTags: [],
fileImports: ["import React from 'react';\n"],
fileComponents: [],
},
],
};
expect(reducer(initialState, createComponent({ text: 'TestFile' }))).toEqual(state);
});
});
describe('setCurrentFile:', () => {
test('should update state', () => {
expect(reducer(initialState, setCurrentFile('TestFile.jsx'))).toEqual({ ...initialState, currentFile: 'TestFile.jsx' });
});
});
});