|
| 1 | +import { |
| 2 | + editFileAndWaitForHmrComplete, |
| 3 | + getColor, |
| 4 | + getEl, |
| 5 | + getText, |
| 6 | + isBuild, |
| 7 | + untilUpdated |
| 8 | +} from '../../testUtils'; |
| 9 | + |
| 10 | +import fetch from 'node-fetch'; |
| 11 | + |
| 12 | +describe('kit-node', () => { |
| 13 | + describe('index route', () => { |
| 14 | + it('should contain greeting', async () => { |
| 15 | + // TODO is hydration testing needed here? |
| 16 | + expect(await page.textContent('h1')).toMatch('Hello world!'); // after hydration |
| 17 | + |
| 18 | + const html = await (await fetch(page.url())).text(); |
| 19 | + expect(html).toMatch('Hello world!'); // before hydration |
| 20 | + if (isBuild) { |
| 21 | + // TODO additional testing needed here once vite-plugin-svelte implements indexHtmlTransform hook |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + it('should have correct styles applied', async () => { |
| 26 | + if (isBuild) { |
| 27 | + expect(await getColor('h1')).toBe('rgb(255, 62, 0)'); |
| 28 | + } else { |
| 29 | + // During dev, the CSS is loaded from async chunk and we may have to wait |
| 30 | + // when the test runs concurrently. |
| 31 | + await untilUpdated(() => getColor('h1'), 'rgb(255, 62, 0)'); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + it('should increase count on click', async () => { |
| 36 | + const button = await getEl('button'); |
| 37 | + expect(await getText(button)).toBe('Clicks: 0'); |
| 38 | + await button.click(); |
| 39 | + expect(await getText(button)).toBe('Clicks: 1'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should not have failed requests', async () => { |
| 43 | + // should have no 404s |
| 44 | + browserLogs.forEach((msg) => { |
| 45 | + expect(msg).not.toMatch('404'); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + if (!isBuild) { |
| 50 | + describe('hmr', () => { |
| 51 | + const updateIndexSvelte = editFileAndWaitForHmrComplete.bind( |
| 52 | + null, |
| 53 | + 'src/routes/index.svelte' |
| 54 | + ); |
| 55 | + |
| 56 | + it('should render additional html', async () => { |
| 57 | + // add div 1 |
| 58 | + expect(await getEl('#hmr-test')).toBe(null); |
| 59 | + await updateIndexSvelte((content) => |
| 60 | + content.replace( |
| 61 | + '<!-- HMR-TEMPLATE-INJECT -->', |
| 62 | + '<div id="hmr-test">foo</div>\n<!-- HMR-TEMPLATE-INJECT -->' |
| 63 | + ) |
| 64 | + ); |
| 65 | + await expect(await getText(`#hmr-test`)).toBe('foo'); |
| 66 | + |
| 67 | + // add div 2 |
| 68 | + expect(await getEl('#hmr-test2')).toBe(null); |
| 69 | + await updateIndexSvelte((content) => |
| 70 | + content.replace( |
| 71 | + '<!-- HMR-TEMPLATE-INJECT -->', |
| 72 | + '<div id="hmr-test2">bar</div>\n<!-- HMR-TEMPLATE-INJECT -->' |
| 73 | + ) |
| 74 | + ); |
| 75 | + await expect(await getText(`#hmr-test`)).toBe('foo'); |
| 76 | + await expect(await getText(`#hmr-test2`)).toBe('bar'); |
| 77 | + // remove div 1 |
| 78 | + await updateIndexSvelte((content) => |
| 79 | + content.replace('<div id="hmr-test">foo</div>\n', '') |
| 80 | + ); |
| 81 | + await expect(await getText(`#hmr-test`)).toBe(null); |
| 82 | + await expect(await getText(`#hmr-test2`)).toBe('bar'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should render additional child components', async () => { |
| 86 | + let buttons = await page.$$('button'); |
| 87 | + expect(buttons.length).toBe(1); |
| 88 | + expect(await getText(buttons[0])).toBe('Clicks: 0'); |
| 89 | + await updateIndexSvelte((content) => |
| 90 | + content.replace( |
| 91 | + '<!-- HMR-TEMPLATE-INJECT -->', |
| 92 | + '<Counter id="hmr-test-counter"/>\n<!-- HMR-TEMPLATE-INJECT -->' |
| 93 | + ) |
| 94 | + ); |
| 95 | + buttons = await page.$$('button'); |
| 96 | + expect(buttons.length).toBe(2); |
| 97 | + expect(await getText(buttons[0])).toBe('Clicks: 0'); |
| 98 | + expect(await getText(buttons[1])).toBe('Clicks: 0'); |
| 99 | + await buttons[1].click(); |
| 100 | + expect(await getText(buttons[0])).toBe('Clicks: 0'); |
| 101 | + expect(await getText(buttons[1])).toBe('Clicks: 1'); |
| 102 | + await updateIndexSvelte((content) => |
| 103 | + content.replace('<Counter id="hmr-test-counter"/>\n', '') |
| 104 | + ); |
| 105 | + buttons = await page.$$('button'); |
| 106 | + expect(buttons.length).toBe(1); |
| 107 | + expect(await getText(buttons[0])).toBe('Clicks: 0'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should apply changed styles', async () => { |
| 111 | + expect(await getColor(`h1`)).toBe('rgb(255, 62, 0)'); |
| 112 | + await updateIndexSvelte((content) => content.replace('color: #ff3e00', 'color: blue')); |
| 113 | + expect(await getColor(`h1`)).toBe('blue'); |
| 114 | + await updateIndexSvelte((content) => content.replace('color: blue', 'color: green')); |
| 115 | + expect(await getColor(`h1`)).toBe('green'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should serve changes even after page reload', async () => { |
| 119 | + expect(await getColor(`h1`)).toBe('green'); |
| 120 | + expect(await getText(`#hmr-test2`)).toBe('bar'); |
| 121 | + await page.reload({ waitUntil: 'networkidle' }); |
| 122 | + expect(await getColor(`h1`)).toBe('green'); |
| 123 | + await expect(await getText(`#hmr-test2`)).toBe('bar'); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('Counter.svelte', () => { |
| 127 | + const updateCounter = editFileAndWaitForHmrComplete.bind(null, 'src/lib/Counter.svelte'); |
| 128 | + it('should render additional html', async () => { |
| 129 | + // add div 1 |
| 130 | + expect(await getEl('#hmr-test3')).toBe(null); |
| 131 | + await updateCounter((content) => |
| 132 | + content.replace( |
| 133 | + '<!-- HMR-TEMPLATE-INJECT -->', |
| 134 | + '<div id="hmr-test3">foo</div>\n<!-- HMR-TEMPLATE-INJECT -->' |
| 135 | + ) |
| 136 | + ); |
| 137 | + await expect(await getText(`#hmr-test3`)).toBe('foo'); |
| 138 | + |
| 139 | + // add div 2 |
| 140 | + expect(await getEl('#hmr-test4')).toBe(null); |
| 141 | + await updateCounter((content) => |
| 142 | + content.replace( |
| 143 | + '<!-- HMR-TEMPLATE-INJECT -->', |
| 144 | + '<div id="hmr-test4">bar</div>\n<!-- HMR-TEMPLATE-INJECT -->' |
| 145 | + ) |
| 146 | + ); |
| 147 | + await expect(await getText(`#hmr-test3`)).toBe('foo'); |
| 148 | + await expect(await getText(`#hmr-test4`)).toBe('bar'); |
| 149 | + // remove div 1 |
| 150 | + await updateCounter((content) => |
| 151 | + content.replace('<div id="hmr-test3">foo</div>\n', '') |
| 152 | + ); |
| 153 | + await expect(await getText(`#hmr-test3`)).toBe(null); |
| 154 | + await expect(await getText(`#hmr-test4`)).toBe('bar'); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should apply changed styles', async () => { |
| 158 | + expect(await getColor(`button`)).toBe('rgb(255, 62, 0)'); |
| 159 | + await updateCounter((content) => content.replace('color: #ff3e00', 'color: blue')); |
| 160 | + expect(await getColor(`button`)).toBe('blue'); |
| 161 | + await updateCounter((content) => content.replace('color: blue', 'color: green')); |
| 162 | + expect(await getColor(`button`)).toBe('green'); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should apply changed initial state', async () => { |
| 166 | + expect(await getText('button')).toBe('Clicks: 0'); |
| 167 | + await updateCounter((content) => content.replace('let count = 0', 'let count = 2')); |
| 168 | + expect(await getText('button')).toBe('Clicks: 2'); |
| 169 | + await updateCounter((content) => content.replace('let count = 2', 'let count = 0')); |
| 170 | + expect(await getText('button')).toBe('Clicks: 0'); |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | + } |
| 175 | + }); |
| 176 | +}); |
0 commit comments