ui.spec.js
5.75 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
/**
* @author NHN Ent. FE Development Team <dl_javascript@nhn.com>
* @fileoverview Test cases of "src/js/ui.js"
*/
import snippet from 'tui-code-snippet';
import { Promise } from '../src/js/util';
import UI from '../src/js/ui';
import { HELP_MENUS } from '../src/js/consts';
describe('UI', () => {
let ui;
let uiOptions;
beforeEach(() => {
uiOptions = {
loadImage: {
path: 'mockImagePath',
name: '',
},
menu: ['crop', 'flip', 'rotate', 'draw', 'shape', 'icon', 'text', 'mask', 'filter'],
initMenu: 'shape',
menuBarPosition: 'bottom',
};
ui = new UI(document.createElement('div'), uiOptions, {});
});
describe('Destroy()', () => {
it('"_destroyAllMenu()" The "destroy" function of all menu instances must be executed.', () => {
snippet.forEach(uiOptions.menu, (menuName) => {
spyOn(ui[menuName], 'destroy');
});
ui._destroyAllMenu();
snippet.forEach(uiOptions.menu, (menuName) => {
expect(ui[menuName].destroy).toHaveBeenCalled();
});
});
it('"_removeUiEvent()" must execute "removeEventListener" of all menus.', () => {
const allUiButtonElementName = [...uiOptions.menu, ...HELP_MENUS];
snippet.forEach(allUiButtonElementName, (elementName) => {
spyOn(ui._buttonElements[elementName], 'removeEventListener');
});
ui._removeUiEvent();
snippet.forEach(allUiButtonElementName, (elementName) => {
expect(ui._buttonElements[elementName].removeEventListener).toHaveBeenCalled();
});
});
});
describe('_changeMenu()', () => {
beforeEach(() => {
ui.submenu = 'shape';
spyOn(ui, 'resizeEditor');
spyOn(ui.shape, 'changeStandbyMode');
spyOn(ui.filter, 'changeStartMode');
ui._actions.main = {
changeSelectableAll: jasmine.createSpy('changeSelectableAll'),
};
ui._changeMenu('filter', false, false);
});
it('When the menu changes, the changeStartMode () of the menu instance to be changed must be executed.', () => {
expect(ui.shape.changeStandbyMode).toHaveBeenCalled();
});
it('When the menu changes, the changeStandbyMode () of the existing menu instance must be executed.', () => {
expect(ui.filter.changeStartMode).toHaveBeenCalled();
});
});
describe('_makeSubMenu()', () => {
it('MakeMenuElement should be executed for the number of menus specified in the option.', () => {
spyOn(ui, '_makeMenuElement');
ui._makeSubMenu();
expect(ui._makeMenuElement.calls.count()).toBe(uiOptions.menu.length);
});
it('Instance of the menu specified in the option must be created.', () => {
spyOn(ui, '_makeMenuElement');
const getConstructorName = (constructor) =>
constructor.toString().match(/^function\s(.+?)\(/)[1];
ui._makeSubMenu();
snippet.forEach(uiOptions.menu, (menuName) => {
const constructorNameOfInstance = getConstructorName(ui[menuName].constructor);
const expected = menuName.replace(/^[a-z]/, ($0) => $0.toUpperCase());
expect(constructorNameOfInstance).toBe(expected);
});
});
});
describe('initCanvas()', () => {
let promise;
beforeEach(() => {
promise = new Promise((resolve) => {
resolve();
});
ui._editorElement = {
querySelector: jasmine
.createSpy('querySelector')
.and.returnValue(document.createElement('div')),
};
ui._actions.main = {
initLoadImage: jasmine.createSpy('initLoadImage').and.returnValue(promise),
};
});
it('When initCanvas is executed, some internal methods must be run as required.', (done) => {
spyOn(ui, 'activeMenuEvent');
spyOn(ui, '_addLoadEvent');
ui.initCanvas();
promise.then(() => {
expect(ui.activeMenuEvent).toHaveBeenCalled();
expect(ui._addLoadEvent).toHaveBeenCalled();
done();
});
});
it('`initLoadImage()` should not be run when has not image path.', () => {
spyOn(ui, '_getLoadImage').and.returnValue({ path: '' });
ui.initCanvas();
expect(ui._actions.main.initLoadImage).not.toHaveBeenCalled();
});
it('`_AddLoadEvent()` should be executed even if there is no image path.', () => {
spyOn(ui, '_getLoadImage').and.returnValue({ path: '' });
spyOn(ui, '_addLoadEvent');
ui.initCanvas();
expect(ui._addLoadEvent).toHaveBeenCalled();
});
});
describe('_setEditorPosition()', () => {
beforeEach(() => {
ui._editorElement = document.createElement('div');
spyOn(ui, '_getCanvasMaxDimension').and.returnValue({
width: 300,
height: 300,
});
});
it('Position is bottom, it should be reflected in the bottom of the editor position.', () => {
ui.submenu = true;
ui._setEditorPosition('bottom');
expect(ui._editorElement.style.top).toBe('150px');
expect(ui._editorElement.style.left).toBe('0px');
});
it('Position is top, it should be reflected in the top of the editor position.', () => {
ui.submenu = true;
ui._setEditorPosition('top');
expect(ui._editorElement.style.top).toBe('-150px');
expect(ui._editorElement.style.left).toBe('0px');
});
it('Position is left, it should be reflected in the left, right of the editor position.', () => {
ui.submenu = true;
ui._setEditorPosition('left');
expect(ui._editorElement.style.top).toBe('0px');
expect(ui._editorElement.style.left).toBe('-150px');
});
it('Position is right, it should be reflected in the right of the editor position.', () => {
ui.submenu = true;
ui._setEditorPosition('right');
expect(ui._editorElement.style.top).toBe('0px');
expect(ui._editorElement.style.left).toBe('150px');
});
});
});