|
| 1 | +import { |
| 2 | + isBuild, |
| 3 | + getEl, |
| 4 | + getText, |
| 5 | + editFileAndWaitForHmrComplete, |
| 6 | + untilUpdated, |
| 7 | + sleep, |
| 8 | + getColor, |
| 9 | + addFile, |
| 10 | + removeFile |
| 11 | +} from '../../testUtils'; |
| 12 | + |
| 13 | +test('should render App', async () => { |
| 14 | + expect(await getText('h1')).toBe(`I'm blue`); |
| 15 | + expect(await getColor('h1')).toBe('blue'); |
| 16 | + expect(await getText('h2')).toBe(`I'm red`); |
| 17 | + expect(await getColor('h2')).toBe('red'); |
| 18 | + expect(await getText('p')).toBe(`I'm green`); |
| 19 | + expect(await getColor('p')).toBe('green'); |
| 20 | + expect(await getText('span')).toBe(`I'm orangered`); |
| 21 | + expect(await getColor('span')).toBe('orangered'); |
| 22 | +}); |
| 23 | + |
| 24 | +test('should not have failed requests', async () => { |
| 25 | + browserLogs.forEach((msg) => { |
| 26 | + expect(msg).not.toMatch('404'); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +if (!isBuild) { |
| 31 | + describe('hmr', () => { |
| 32 | + test('should apply updates when editing App.svelte', async () => { |
| 33 | + expect(await getText('span')).toBe(`I'm orangered`); |
| 34 | + await editFileAndWaitForHmrComplete('src/App.svelte', (c) => |
| 35 | + c.replace(`I'm orangered`, `I'm replaced`) |
| 36 | + ); |
| 37 | + expect(await getText('span')).toBe(`I'm replaced`); |
| 38 | + expect(await getColor('span')).toBe('orangered'); |
| 39 | + await editFileAndWaitForHmrComplete( |
| 40 | + 'src/App.svelte', |
| 41 | + (c) => c.replace(`color: orangered`, `color: magenta`), |
| 42 | + '/src/App.svelte?svelte&type=style&lang.css' |
| 43 | + ); |
| 44 | + expect(await getColor('span')).toBe('magenta'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('should apply updates when editing MultiFile.html', async () => { |
| 48 | + expect(await getText('h1')).toBe(`I'm blue`); |
| 49 | + expect(await getText('h2')).toBe(`I'm red`); |
| 50 | + await editFileAndWaitForHmrComplete( |
| 51 | + 'src/lib/multifile/MultiFile.html', |
| 52 | + (c) => c.replace(`I'm blue`, `I'm replaced`).replace(`I'm red`, `I'm replaced too`), |
| 53 | + '/src/lib/multifile/MultiFile.svelte' |
| 54 | + ); |
| 55 | + expect(await getText('h1')).toBe(`I'm replaced`); |
| 56 | + expect(await getText('h2')).toBe(`I'm replaced too`); |
| 57 | + }); |
| 58 | + |
| 59 | + test('should apply updates when editing MultiFile.scss', async () => { |
| 60 | + expect(await getColor('h1')).toBe('blue'); |
| 61 | + await editFileAndWaitForHmrComplete( |
| 62 | + 'src/lib/multifile/MultiFile.scss', |
| 63 | + (c) => c.replace(`color: blue`, `color: magenta`), |
| 64 | + '/src/lib/multifile/MultiFile.svelte?svelte&type=style&lang.css' |
| 65 | + ); |
| 66 | + expect(await getColor('h1')).toBe('magenta'); |
| 67 | + }); |
| 68 | + |
| 69 | + test('should apply updates when editing _someImport.scss', async () => { |
| 70 | + expect(await getColor('h2')).toBe('red'); |
| 71 | + await editFileAndWaitForHmrComplete( |
| 72 | + 'src/lib/multifile/_someImport.scss', |
| 73 | + (c) => c.replace(`color: red`, `color: magenta`), |
| 74 | + '/src/lib/multifile/MultiFile.svelte?svelte&type=style&lang.css' |
| 75 | + ); |
| 76 | + expect(await getColor('h2')).toBe('magenta'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('should remove styles that are no longer imported', async () => { |
| 80 | + expect(await getColor('h2')).toBe('magenta'); |
| 81 | + await editFileAndWaitForHmrComplete( |
| 82 | + 'src/lib/multifile/MultiFile.scss', |
| 83 | + (c) => c.replace(`@import 'someImport';`, `/*@import 'someImport';*/`), |
| 84 | + '/src/lib/multifile/MultiFile.svelte?svelte&type=style&lang.css' |
| 85 | + ); |
| 86 | + expect(await getColor('h2')).toBe('black'); |
| 87 | + }); |
| 88 | + |
| 89 | + test('should apply styles from new dependency', async () => { |
| 90 | + expect(await getColor('h2')).toBe('black'); |
| 91 | + await addFile('src/lib/multifile/_foo.scss', 'h2 { color: maroon; }'); |
| 92 | + expect(await getColor('h2')).toBe('black'); |
| 93 | + await editFileAndWaitForHmrComplete( |
| 94 | + 'src/lib/multifile/MultiFile.scss', |
| 95 | + (c) => c.replace(`/*@import 'someImport';*/`, `/*@import 'someImport';*/\n@import 'foo';`), |
| 96 | + '/src/lib/multifile/MultiFile.svelte?svelte&type=style&lang.css' |
| 97 | + ); |
| 98 | + expect(await getColor('h2')).toBe('maroon'); |
| 99 | + await editFileAndWaitForHmrComplete( |
| 100 | + 'src/lib/multifile/_foo.scss', |
| 101 | + (c) => c.replace(`maroon`, `green`), |
| 102 | + '/src/lib/multifile/MultiFile.svelte?svelte&type=style&lang.css' |
| 103 | + ); |
| 104 | + expect(await getColor('h2')).toBe('green'); |
| 105 | + }); |
| 106 | + |
| 107 | + test('should apply updates when editing MultiFile.ts', async () => { |
| 108 | + expect(await getText('p')).toBe(`I'm green`); |
| 109 | + await editFileAndWaitForHmrComplete( |
| 110 | + 'src/lib/multifile/MultiFile.ts', |
| 111 | + (c) => c.replace(`'green'`, `'a replaced value'`), |
| 112 | + '/src/lib/multifile/MultiFile.svelte' |
| 113 | + ); |
| 114 | + expect(await getText('p')).toBe(`I'm a replaced value`); |
| 115 | + }); |
| 116 | + |
| 117 | + test('should apply updates when editing someother.css', async () => { |
| 118 | + expect(await getColor('p')).toBe('green'); |
| 119 | + await editFileAndWaitForHmrComplete('src/lib/multifile/someother.css', (c) => |
| 120 | + c.replace(`color: green`, `color: magenta`) |
| 121 | + ); |
| 122 | + expect(await getColor('p')).toBe('magenta'); |
| 123 | + }); |
| 124 | + |
| 125 | + test('should show error on deleting dependency', async () => { |
| 126 | + expect(await getColor('h2')).toBe('green'); |
| 127 | + await removeFile('src/lib/multifile/_foo.scss'); |
| 128 | + expect(await getColor('h2')).toBe('green'); |
| 129 | + const errorOverlay = await page.waitForSelector('vite-error-overlay'); |
| 130 | + expect(errorOverlay).toBeTruthy(); |
| 131 | + await errorOverlay.click({ position: { x: 1, y: 1 } }); |
| 132 | + await sleep(50); |
| 133 | + const errorOverlay2 = await getEl('vite-error-overlay'); |
| 134 | + expect(errorOverlay2).toBeFalsy(); |
| 135 | + await editFileAndWaitForHmrComplete( |
| 136 | + 'src/lib/multifile/MultiFile.scss', |
| 137 | + (c) => c.replace(`@import 'foo';`, ``), |
| 138 | + '/src/lib/multifile/MultiFile.svelte?svelte&type=style&lang.css' |
| 139 | + ); |
| 140 | + expect(await getColor('h2')).toBe('black'); |
| 141 | + }); |
| 142 | + }); |
| 143 | +} |
0 commit comments