|
| 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 | + |
0 commit comments