Skip to content

Commit 126d29e

Browse files
committed
More tests
1 parent 4a18129 commit 126d29e

4 files changed

Lines changed: 401 additions & 0 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import { describe, it, expect, beforeEach } from 'vitest';
2+
import { ref } from 'vue';
3+
import useEditorUtils from '~/composables/useEditorUtils';
4+
import usePreferencesStore from '~/composables/usePreferencesStore';
5+
6+
describe('useEditorUtils', () => {
7+
let preferences;
8+
9+
beforeEach(() => {
10+
localStorage.clear();
11+
12+
preferences = usePreferencesStore();
13+
});
14+
15+
describe('stripInitialPhpTag', () => {
16+
it('strips opening PHP tag', () => {
17+
const { stripInitialPhpTag } = useEditorUtils();
18+
19+
expect(stripInitialPhpTag('<?php\necho "hello";')).toBe('echo "hello";');
20+
});
21+
22+
it('strips opening PHP tag without newline', () => {
23+
const { stripInitialPhpTag } = useEditorUtils();
24+
25+
expect(stripInitialPhpTag('<?php echo "hello";')).toBe(' echo "hello";');
26+
});
27+
28+
it('does not strip PHP tag in the middle', () => {
29+
const { stripInitialPhpTag } = useEditorUtils();
30+
31+
expect(stripInitialPhpTag('echo "hello"; <?php')).toBe('echo "hello"; <?php');
32+
});
33+
});
34+
35+
describe('getCodeFromEditors', () => {
36+
it('returns code from editors', () => {
37+
const { getCodeFromEditors } = useEditorUtils();
38+
39+
const editors = [
40+
{ id: '1', language: 'javascript', value: 'const x = 1;', added: [], removed: [], focused: [] },
41+
];
42+
43+
const result = getCodeFromEditors(editors);
44+
45+
expect(result).toEqual([
46+
{ id: '1', value: 'const x = 1;', added: [], removed: [], focused: [] },
47+
]);
48+
});
49+
50+
it('strips initial PHP tag when preference is enabled', () => {
51+
preferences.stripIntialPhpTag = true;
52+
53+
const { getCodeFromEditors } = useEditorUtils();
54+
55+
const editors = [
56+
{ id: '1', language: 'php', value: '<?php\necho "hello";', added: [2], removed: [3], focused: [4] },
57+
];
58+
59+
const result = getCodeFromEditors(editors);
60+
61+
expect(result[0].value).toBe('echo "hello";');
62+
expect(result[0].added).toEqual([1]);
63+
expect(result[0].removed).toEqual([2]);
64+
expect(result[0].focused).toEqual([3]);
65+
});
66+
67+
it('does not strip PHP tag when preference is disabled', () => {
68+
preferences.stripIntialPhpTag = false;
69+
70+
const { getCodeFromEditors } = useEditorUtils();
71+
72+
const editors = [
73+
{ id: '1', language: 'php', value: '<?php\necho "hello";', added: [2], removed: [], focused: [] },
74+
];
75+
76+
const result = getCodeFromEditors(editors);
77+
78+
expect(result[0].value).toBe('<?php\necho "hello";');
79+
expect(result[0].added).toEqual([2]);
80+
});
81+
82+
it('only strips PHP tag for PHP language', () => {
83+
preferences.stripIntialPhpTag = true;
84+
85+
const { getCodeFromEditors } = useEditorUtils();
86+
87+
const editors = [
88+
{ id: '1', language: 'javascript', value: '<?php\nconst x = 1;', added: [], removed: [], focused: [] },
89+
];
90+
91+
const result = getCodeFromEditors(editors);
92+
93+
expect(result[0].value).toBe('<?php\nconst x = 1;');
94+
});
95+
96+
it('accepts a ref of editors', () => {
97+
const { getCodeFromEditors } = useEditorUtils();
98+
99+
const editors = ref([
100+
{ id: '1', language: 'javascript', value: 'const x = 1;', added: [], removed: [], focused: [] },
101+
]);
102+
103+
const result = getCodeFromEditors(editors);
104+
105+
expect(result).toHaveLength(1);
106+
});
107+
108+
it('handles null added/removed/focused', () => {
109+
const { getCodeFromEditors } = useEditorUtils();
110+
111+
const editors = [
112+
{ id: '1', language: 'javascript', value: 'x', added: null, removed: null, focused: null },
113+
];
114+
115+
const result = getCodeFromEditors(editors);
116+
117+
expect(result[0].added).toEqual([]);
118+
expect(result[0].removed).toEqual([]);
119+
expect(result[0].focused).toEqual([]);
120+
});
121+
});
122+
123+
describe('getLanguagesFromEditors', () => {
124+
it('returns languages from editors', () => {
125+
const { getLanguagesFromEditors } = useEditorUtils();
126+
127+
const editors = [
128+
{ id: '1', language: 'php', value: '' },
129+
{ id: '2', language: 'javascript', value: '' },
130+
];
131+
132+
expect(getLanguagesFromEditors(editors)).toEqual([
133+
{ id: '1', name: 'php' },
134+
{ id: '2', name: 'javascript' },
135+
]);
136+
});
137+
});
138+
});
139+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { describe, it, expect, beforeEach } from 'vitest';
2+
import useProjectStoreFactory from '~/composables/useProjectStoreFactory';
3+
import useTemplateStore from '~/composables/useTemplateStore';
4+
import { namespace } from '~/composables/useProjectStores';
5+
6+
describe('useProjectStoreFactory', () => {
7+
beforeEach(() => {
8+
localStorage.clear();
9+
10+
useTemplateStore().$reset();
11+
});
12+
13+
it('creates a store with default state', () => {
14+
const store = useProjectStoreFactory(`${namespace}test-id`)();
15+
16+
expect(store.tab.id).toBe('test-id');
17+
expect(store.tab.name).toBeNull();
18+
expect(store.tab.order).toBe(0);
19+
expect(store.modified).toBe(false);
20+
});
21+
22+
it('strips the namespace from the tab ID', () => {
23+
const store = useProjectStoreFactory(`${namespace}my-project`)();
24+
25+
expect(store.tab.id).toBe('my-project');
26+
});
27+
28+
it('clones a project with a new tab ID', () => {
29+
const store = useProjectStoreFactory(`${namespace}original`)();
30+
31+
store.tab.name = 'My Project';
32+
33+
const clone = store.clone();
34+
35+
expect(clone.tab.id).not.toBe('original');
36+
expect(clone.tab.name).toBe('My Project');
37+
});
38+
39+
it('saves as a template', () => {
40+
const templates = useTemplateStore();
41+
const store = useProjectStoreFactory(`${namespace}test`)();
42+
43+
store.tab.name = 'Test Project';
44+
45+
store.saveAsTemplate();
46+
47+
expect(templates.all()).toHaveLength(1);
48+
expect(templates.all()[0].tab.name).toBe('Test Project');
49+
expect(templates.all()[0].tab.id).not.toBe('test');
50+
});
51+
52+
it('uses "Untitled Project" when saving a nameless project as template', () => {
53+
const templates = useTemplateStore();
54+
const store = useProjectStoreFactory(`${namespace}nameless-test`)();
55+
56+
store.saveAsTemplate();
57+
58+
const saved = templates.all().find((t) => t.tab.name === 'Untitled Project');
59+
60+
expect(saved).toBeTruthy();
61+
});
62+
});
63+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { describe, it, expect, beforeEach } from 'vitest';
2+
import useSettingsStore from '~/composables/useSettingsStore';
3+
4+
describe('useSettingsStore', () => {
5+
let settings;
6+
7+
beforeEach(() => {
8+
localStorage.clear();
9+
10+
settings = useSettingsStore();
11+
12+
settings.$reset();
13+
});
14+
15+
it('starts with empty state', () => {
16+
expect(settings.tab).toBe('');
17+
expect(settings.defaultTemplate).toBeNull();
18+
expect(settings.backgrounds).toEqual([]);
19+
});
20+
21+
it('adds a background', () => {
22+
settings.addBackground('bg-1', { class: 'bg-red-500' });
23+
24+
expect(settings.backgrounds).toHaveLength(1);
25+
expect(settings.backgrounds[0]).toEqual({ id: 'bg-1', class: 'bg-red-500' });
26+
});
27+
28+
it('adds multiple backgrounds', () => {
29+
settings.addBackground('bg-1', { class: 'bg-red-500' });
30+
settings.addBackground('bg-2', { class: 'bg-blue-500' });
31+
32+
expect(settings.backgrounds).toHaveLength(2);
33+
});
34+
35+
it('deletes a background', () => {
36+
settings.addBackground('bg-1', { class: 'bg-red-500' });
37+
settings.addBackground('bg-2', { class: 'bg-blue-500' });
38+
39+
settings.deleteBackground('bg-1');
40+
41+
expect(settings.backgrounds).toHaveLength(1);
42+
expect(settings.backgrounds[0].id).toBe('bg-2');
43+
});
44+
45+
it('returns displayable backgrounds with custom flag', () => {
46+
settings.addBackground('bg-1', { class: 'bg-red-500' });
47+
48+
const displayable = settings.getDisplayableBackgrounds();
49+
50+
expect(displayable).toHaveLength(1);
51+
expect(displayable[0]).toEqual({
52+
id: 0,
53+
custom: true,
54+
class: 'bg-red-500',
55+
'id': 'bg-1',
56+
});
57+
});
58+
59+
it('sets a default template', () => {
60+
settings.setDefaultTemplate('template-1');
61+
62+
expect(settings.getDefaultTemplate()).toBe('template-1');
63+
});
64+
65+
it('clears the default template', () => {
66+
settings.setDefaultTemplate('template-1');
67+
68+
settings.clearDefaultTemplate();
69+
70+
expect(settings.getDefaultTemplate()).toBeNull();
71+
});
72+
});
73+

0 commit comments

Comments
 (0)