generated from hemengke1997/ts-starter
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ef6e81
commit de53a39
Showing
23 changed files
with
75 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,18 @@ | ||
import fetch from 'node-fetch' | ||
import { describe, expect, test } from 'vitest' | ||
import { beforeAll, describe, expect, test } from 'vitest' | ||
import { port } from './serve' | ||
import { browserLogs, editFile, page, untilBrowserLogAfter, untilUpdated } from '~utils' | ||
import { browserLogs, page } from '~utils' | ||
|
||
const url = `http://localhost:${port}` | ||
|
||
describe('ssr-react', () => { | ||
test('/env', async () => { | ||
await untilBrowserLogAfter(() => page.goto(`${url}/env`), 'hydrated') | ||
|
||
expect(await page.textContent('h1')).toMatch('default message here') | ||
|
||
// raw http request | ||
const envHtml = await (await fetch(`${url}/env`)).text() | ||
expect(envHtml).toMatch('API_KEY_qwertyuiop') | ||
}) | ||
|
||
test('/about', async () => { | ||
await untilBrowserLogAfter(() => page.goto(`${url}/about`), 'hydrated') | ||
|
||
expect(await page.textContent('h1')).toMatch('About') | ||
// should not have hydration mismatch | ||
browserLogs.forEach((msg) => { | ||
expect(msg).not.toMatch('Expected server HTML') | ||
}) | ||
|
||
// raw http request | ||
const aboutHtml = await (await fetch(`${url}/about`)).text() | ||
expect(aboutHtml).toMatch('About') | ||
}) | ||
|
||
test('/', async () => { | ||
await untilBrowserLogAfter(() => page.goto(url), 'hydrated') | ||
|
||
expect(await page.textContent('h1')).toMatch('Home') | ||
// should not have hydration mismatch | ||
browserLogs.forEach((msg) => { | ||
expect(msg).not.toMatch('Expected server HTML') | ||
}) | ||
|
||
// raw http request | ||
const html = await (await fetch(url)).text() | ||
expect(html).toMatch('Home') | ||
}) | ||
|
||
test('hmr', async () => { | ||
await untilBrowserLogAfter(() => page.goto(url), 'hydrated') | ||
|
||
editFile('src/pages/Home.jsx', (code) => code.replace('<h1>Home', '<h1>changed')) | ||
await untilUpdated(() => page.textContent('h1'), 'changed') | ||
}) | ||
|
||
test('client navigation', async () => { | ||
await untilBrowserLogAfter(() => page.goto(url), 'hydrated') | ||
|
||
await untilUpdated(() => page.textContent('a[href="/about"]'), 'About') | ||
await page.click('a[href="/about"]') | ||
await untilUpdated(() => page.textContent('h1'), 'About') | ||
editFile('src/pages/About.jsx', (code) => code.replace('<h1>About', '<h1>changed')) | ||
await untilUpdated(() => page.textContent('h1'), 'changed') | ||
}) | ||
}) | ||
|
||
describe('ssr - console', () => { | ||
beforeAll(async () => {}) | ||
|
||
function expectBrowserLogsToContain(str: string) { | ||
expect(browserLogs).toEqual(expect.arrayContaining([expect.stringContaining(str)])) | ||
} | ||
|
||
test('should console `this is ssr`', async () => { | ||
await page.goto(url) | ||
expectBrowserLogsToContain('this is ssr') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite App</title> | ||
</head> | ||
<body> | ||
<div id="app"><!--app-html--></div> | ||
<script type="module" src="/src/entry-client.jsx"></script> | ||
</body> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite App</title> | ||
</head> | ||
|
||
<body> | ||
<div id="app"><!--app-html--></div> | ||
<script type="module" src="/src/entry-client.tsx"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function App() { | ||
return ( | ||
<> | ||
<div>this is app</div> | ||
</> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import ReactDOM from 'react-dom/client' | ||
import App from './App' | ||
|
||
ReactDOM.hydrateRoot(document.querySelector('#app'), <App />) | ||
console.log('hydrated') |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import ReactDOMServer from 'react-dom/server' | ||
import App from './App' | ||
|
||
export function render() { | ||
return ReactDOMServer.renderToString(<App />) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.