-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(request-nanostores): Test documented behavior (#162)
- Loading branch information
Showing
22 changed files
with
404 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { loadFixture, type PreviewServer } from '@inox-tools/astro-tests/astroFixture'; | ||
import { test, expect } from '@playwright/test'; | ||
|
||
const fixture = await loadFixture({ | ||
root: './fixture/basic', | ||
}); | ||
|
||
let server: PreviewServer; | ||
|
||
test.beforeAll(async () => { | ||
delete process.env.INJECTED_STATE; | ||
await fixture.build({}); | ||
process.env.INJECTED_STATE = JSON.stringify({ | ||
foo: 'bar', | ||
}); | ||
server = await fixture.preview({}); | ||
}); | ||
|
||
test.afterAll(async () => { | ||
await server?.stop(); | ||
delete process.env.INJECTED_STATE; | ||
}); | ||
|
||
test('state is injected from the server on the client', async ({ page }) => { | ||
await page.goto(fixture.resolveUrl('/')); | ||
|
||
const pageState = JSON.parse(await page.locator('pre#injected-state').innerHTML()); | ||
|
||
expect(pageState).toStrictEqual({ foo: 'bar' }); | ||
}); | ||
|
||
test('state is available to UI frameworks in sync with server', async ({ page }) => { | ||
await page.goto(fixture.resolveUrl('/islands')); | ||
|
||
const serverState = JSON.parse(await page.locator('pre#server').innerHTML()); | ||
const clientState = JSON.parse(await page.locator('pre#client').innerHTML()); | ||
|
||
expect(serverState).toStrictEqual(clientState); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/request-nanostores/e2e/fixture/basic/astro.config.ts
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,12 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import node from '@astrojs/node'; | ||
import requestNanostores from '@inox-tools/request-nanostores'; | ||
|
||
import preact from '@astrojs/preact'; | ||
|
||
export default defineConfig({ | ||
output: 'server', | ||
adapter: node({ mode: 'standalone' }), | ||
integrations: [requestNanostores(), preact()], | ||
devToolbar: { enabled: false }, | ||
}); |
14 changes: 14 additions & 0 deletions
14
packages/request-nanostores/e2e/fixture/basic/package.json
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,14 @@ | ||
{ | ||
"name": "@request-nanostores/basic", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"@astrojs/node": "catalog:", | ||
"@astrojs/preact": "^3.5.3", | ||
"@inox-tools/request-nanostores": "workspace:", | ||
"@nanostores/preact": "^0.5.2", | ||
"astro": "catalog:", | ||
"nanostores": "catalog:", | ||
"preact": "^10.24.0" | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/request-nanostores/e2e/fixture/basic/src/components/Display.tsx
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,9 @@ | ||
import type { FunctionComponent } from 'preact'; | ||
import { useStore } from '@nanostores/preact'; | ||
import { serializedState } from '../state.js'; | ||
|
||
export const Display: FunctionComponent<{ id: string }> = ({ id }) => { | ||
const state = useStore(serializedState); | ||
|
||
return <pre id={id}>{state}</pre>; | ||
}; |
23 changes: 23 additions & 0 deletions
23
packages/request-nanostores/e2e/fixture/basic/src/pages/index.astro
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,23 @@ | ||
--- | ||
import { state } from '../state.js'; | ||
const injectedState = JSON.parse(process.env.INJECTED_STATE || '[123]'); | ||
state.set(injectedState); | ||
--- | ||
|
||
<html> | ||
<head> | ||
<title>Basic example</title> | ||
</head> | ||
<body> | ||
<pre id="injected-state"></pre> | ||
<script> | ||
import { serializedState } from '../state.js'; | ||
|
||
serializedState.subscribe((value) => { | ||
document.getElementById('injected-state')!.innerHTML = value; | ||
}); | ||
</script> | ||
</body> | ||
</html> |
23 changes: 23 additions & 0 deletions
23
packages/request-nanostores/e2e/fixture/basic/src/pages/islands.astro
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,23 @@ | ||
--- | ||
import { Display } from '../components/Display.tsx'; | ||
import { state } from '../state.js'; | ||
state.set({ | ||
obj: { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}, | ||
array: [1, 2, 3], | ||
}); | ||
--- | ||
|
||
<html> | ||
<head> | ||
<title>Islands example</title> | ||
</head> | ||
<body> | ||
<Display id="server" /> | ||
<Display id="client" client:only="preact" /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { atom, computed } from 'nanostores'; | ||
import { shared } from '@it-astro:request-nanostores'; | ||
|
||
export const state = shared('fancy-state', atom<any>(null)); | ||
|
||
export const serializedState = computed(state, (state) => JSON.stringify(state)); |
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,9 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"extends": "../../../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"lib": ["dom"], | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "preact" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
defineConfig, | ||
devices, | ||
type Project, | ||
type PlaywrightWorkerOptions, | ||
} from '@playwright/test'; | ||
|
||
type Proj = Project<{}, PlaywrightWorkerOptions>; | ||
|
||
const browserOptions: Record<string, Proj['use']> = { | ||
chromium: { | ||
...devices['Desktop Chrome'], | ||
launchOptions: { | ||
executablePath: process.env.PLAYWRIGHT_CHROME_BIN, | ||
}, | ||
}, | ||
firefox: { | ||
...devices['Desktop Firefox'], | ||
launchOptions: { | ||
executablePath: process.env.PLAYWRIGHT_FIREFOX_BIN, | ||
}, | ||
}, | ||
webkit: { | ||
...devices['Desktop Safari'], | ||
launchOptions: { | ||
executablePath: process.env.PLAYWRIGHT_WEBKIT_BIN, | ||
}, | ||
}, | ||
}; | ||
|
||
const browsers: Record<string, Proj> = Object.fromEntries( | ||
Object.entries(browserOptions).map(([key, value]) => [ | ||
key, | ||
{ | ||
name: key, | ||
use: value, | ||
}, | ||
]) | ||
); | ||
|
||
const projects: Proj[] = [browsers.chromium]; | ||
|
||
if (process.env.CI) { | ||
projects.push(browsers.firefox, browsers.webkit); | ||
} else { | ||
if (process.env.PLAYWRIGHT_FIREFOX_RUN) { | ||
projects.push(browsers.firefox); | ||
} | ||
|
||
if (process.env.PLAYWRIGHT_WEBKIT_RUN) { | ||
projects.push(browsers.webkit); | ||
} | ||
} | ||
|
||
/** | ||
* See https://playwright.dev/docs/test-configuration. | ||
*/ | ||
export default defineConfig({ | ||
testDir: './e2e', | ||
/* Run tests in files in parallel */ | ||
fullyParallel: true, | ||
/* Fail the build on CI if you accidentally left test.only in the source code. */ | ||
forbidOnly: !!process.env.CI, | ||
/* Retry on CI only */ | ||
retries: process.env.CI ? 2 : 0, | ||
/* Opt out of parallel tests on CI. */ | ||
workers: process.env.CI ? 1 : undefined, | ||
/* Reporter to use. See https://playwright.dev/docs/test-reporters */ | ||
reporter: 'html', | ||
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ | ||
use: { | ||
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ | ||
trace: 'on-first-retry', | ||
}, | ||
|
||
/* Configure projects for major browsers */ | ||
projects: projects, | ||
}); |
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
packages/request-nanostores/tests/fixture/race/astro.config.ts
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,8 @@ | ||
import { defineConfig } from 'astro/config'; | ||
import requestNanostores from '@inox-tools/request-nanostores'; | ||
|
||
export default defineConfig({ | ||
output: 'server', | ||
integrations: [requestNanostores()], | ||
devToolbar: { enabled: false }, | ||
}); |
11 changes: 11 additions & 0 deletions
11
packages/request-nanostores/tests/fixture/race/package.json
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,11 @@ | ||
{ | ||
"name": "@request-nanostores/race", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"@astrojs/preact": "^3.5.3", | ||
"@inox-tools/request-nanostores": "workspace:", | ||
"astro": "catalog:", | ||
"nanostores": "catalog:" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/request-nanostores/tests/fixture/race/src/pages/race.ts
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,18 @@ | ||
import type { APIRoute } from 'astro'; | ||
import { identifier } from '../state.js'; | ||
import * as timers from 'node:timers/promises'; | ||
|
||
let requestCount = 0; | ||
|
||
export const GET: APIRoute = async (ctx) => { | ||
const requestId = ++requestCount; | ||
const delay = Number.parseInt(ctx.url.searchParams.get('delay') as string) || 1000; | ||
|
||
identifier.set(requestId); | ||
|
||
await timers.setTimeout(delay); | ||
|
||
const afterDelay = identifier.get(); | ||
|
||
return Response.json({ requestId, afterDelay }); | ||
}; |
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,4 @@ | ||
import { atom } from 'nanostores'; | ||
import { shared } from '@it-astro:request-nanostores'; | ||
|
||
export const identifier = shared('identifier', atom<number>(0)); |
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,9 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/tsconfig", | ||
"extends": "../../../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"lib": ["dom"], | ||
"jsx": "react-jsx", | ||
"jsxImportSource": "preact" | ||
} | ||
} |
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,49 @@ | ||
import { loadFixture } from '@inox-tools/astro-tests/astroFixture'; | ||
import testAdapter from '@inox-tools/astro-tests/testAdapter'; | ||
import { test, expect } from 'vitest'; | ||
|
||
const fixture = await loadFixture({ | ||
root: './fixture/race', | ||
adapter: testAdapter(), | ||
}); | ||
|
||
await fixture.build({}); | ||
const app = await fixture.loadTestAdapterApp(); | ||
|
||
test('concurrent requests have independent state', async () => { | ||
const responses = await Promise.all( | ||
Array(5) | ||
.fill(null) | ||
.map(async (_, index) => { | ||
const delay = 200 * (index + 1); | ||
const url = new URL('https://example.com/race'); | ||
url.searchParams.set('delay', delay.toFixed(0)); | ||
|
||
const response = await app.render(new Request(url)); | ||
return response.json(); | ||
}) | ||
); | ||
|
||
expect(responses).toEqual([ | ||
{ | ||
requestId: 1, | ||
afterDelay: 1, | ||
}, | ||
{ | ||
requestId: 2, | ||
afterDelay: 2, | ||
}, | ||
{ | ||
requestId: 3, | ||
afterDelay: 3, | ||
}, | ||
{ | ||
requestId: 4, | ||
afterDelay: 4, | ||
}, | ||
{ | ||
requestId: 5, | ||
afterDelay: 5, | ||
}, | ||
]); | ||
}); |
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,4 +1,6 @@ | ||
import * as matchers from 'jest-extended'; | ||
import { expect } from 'vitest'; | ||
|
||
process.setSourceMapsEnabled(true); | ||
|
||
expect.extend(matchers); |
Oops, something went wrong.