-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: adds tests as example and for plumbing different things (#8)
* chore: adds tests for cookie context * chore: adds page test and serverside props
- Loading branch information
Showing
11 changed files
with
892 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { render, cleanup } from '@testing-library/react'; | ||
import { afterEach, expect, it, describe } from 'vitest'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { | ||
AppCookiesProvider, | ||
AppCookiesProviderProps, | ||
useCookieSettings, | ||
useDarkmodeCookie, | ||
useLanguageCookie, | ||
} from '../cookies-context'; | ||
|
||
afterEach(function () { | ||
cleanup(); | ||
}); | ||
|
||
type RenderProps = { providerProps: AppCookiesProviderProps }; | ||
const customRender = ( | ||
ui: React.ReactNode, | ||
{ providerProps, ...renderOptions }: RenderProps, | ||
) => { | ||
return render( | ||
<AppCookiesProvider {...providerProps}>{ui}</AppCookiesProvider>, | ||
renderOptions, | ||
); | ||
}; | ||
|
||
describe('cookie contexts', function () { | ||
it('Should provide default state as passed in', () => { | ||
const providerProps: AppCookiesProviderProps = { | ||
initialCookies: { | ||
darkmode: true, | ||
language: 'no', | ||
}, | ||
}; | ||
const TestConsumer = function () { | ||
const state = useCookieSettings(); | ||
|
||
return ( | ||
<div> | ||
<p>Darkmode: {String(state?.darkmode[0]) ?? ''}</p> | ||
<p>Language: {state?.language[0] ?? ''}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
const output = customRender(<TestConsumer />, { providerProps }); | ||
expect(output.getByText(/^Darkmode:/).textContent).toBe('Darkmode: true'); | ||
expect(output.getByText(/^Language:/).textContent).toBe('Language: no'); | ||
}); | ||
|
||
describe('language', function () { | ||
it('Should language cookie', () => { | ||
const providerProps: AppCookiesProviderProps = { | ||
initialCookies: { | ||
darkmode: true, | ||
language: 'no', | ||
}, | ||
}; | ||
const TestConsumer = function () { | ||
const [lang] = useLanguageCookie(); | ||
|
||
return ( | ||
<div> | ||
<p>Language: {lang ?? ''}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
const output = customRender(<TestConsumer />, { providerProps }); | ||
expect(output.getByText(/^Language:/).textContent).toBe('Language: no'); | ||
}); | ||
|
||
it('Should language cookie', async () => { | ||
const providerProps: AppCookiesProviderProps = { | ||
initialCookies: { | ||
darkmode: true, | ||
language: 'no', | ||
}, | ||
}; | ||
const TestConsumer = function () { | ||
const [lang, setLanguage] = useLanguageCookie(); | ||
|
||
return ( | ||
<div> | ||
<p>Language: {lang ?? ''}</p> | ||
<button onClick={() => setLanguage('en')}>Change</button> | ||
</div> | ||
); | ||
}; | ||
|
||
const output = customRender(<TestConsumer />, { providerProps }); | ||
expect(output.getByText(/^Language:/).textContent).toBe('Language: no'); | ||
|
||
const user = userEvent.setup(); | ||
await user.click(output.getByRole('button')); | ||
expect(output.getByText(/^Language:/).textContent).toBe('Language: en'); | ||
}); | ||
}); | ||
|
||
describe('darkmode', function () { | ||
it('Should darkmode cookie', () => { | ||
const providerProps: AppCookiesProviderProps = { | ||
initialCookies: { | ||
darkmode: true, | ||
language: 'no', | ||
}, | ||
}; | ||
const TestConsumer = function () { | ||
const [darkmode] = useDarkmodeCookie(); | ||
|
||
return ( | ||
<div> | ||
<p>Darkmode: {String(darkmode) ?? ''}</p> | ||
</div> | ||
); | ||
}; | ||
|
||
const output = customRender(<TestConsumer />, { providerProps }); | ||
expect(output.getByText(/^Darkmode:/).textContent).toBe('Darkmode: true'); | ||
}); | ||
|
||
it('Should language cookie', async () => { | ||
const providerProps: AppCookiesProviderProps = { | ||
initialCookies: { | ||
darkmode: true, | ||
language: 'no', | ||
}, | ||
}; | ||
const TestConsumer = function () { | ||
const [darkmode, setDarkmode] = useDarkmodeCookie(); | ||
|
||
return ( | ||
<div> | ||
<p>Darkmode: {String(darkmode) ?? ''}</p> | ||
<button onClick={() => setDarkmode(false)}>Change</button> | ||
</div> | ||
); | ||
}; | ||
|
||
const output = customRender(<TestConsumer />, { providerProps }); | ||
expect(output.getByText(/^Darkmode:/).textContent).toBe('Darkmode: true'); | ||
|
||
const user = userEvent.setup(); | ||
await user.click(output.getByRole('button')); | ||
expect(output.getByText(/^Darkmode:/).textContent).toBe( | ||
'Darkmode: false', | ||
); | ||
}); | ||
}); | ||
}); |
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,58 @@ | ||
import { cleanup, render } from '@testing-library/react'; | ||
import { afterEach, describe, expect, it } from 'vitest'; | ||
import { HttpClient } from '@atb/modules/api-server'; | ||
import { AutocompleteApi } from '@atb/page-modules/departures/server/autocomplete'; | ||
import { expectProps, getServerSidePropsWithClient } from '@atb/tests/utils'; | ||
|
||
import DeparturesPage, { | ||
DeparturesPageProps, | ||
getServerSideProps, | ||
} from '@atb/pages/departures'; | ||
|
||
afterEach(function () { | ||
cleanup(); | ||
}); | ||
|
||
describe('departure page', function () { | ||
it('Should show list as provided from initial props ', async () => { | ||
const initialProps: DeparturesPageProps = { | ||
autocompleteFeatures: [ | ||
{ | ||
name: 'Result 1', | ||
}, | ||
{ | ||
name: 'Result 2', | ||
}, | ||
], | ||
headersAcceptLanguage: 'en', | ||
initialCookies: { | ||
darkmode: false, | ||
language: 'en', | ||
}, | ||
}; | ||
|
||
const output = render(<DeparturesPage {...initialProps} />); | ||
expect(await output.findAllByText(/^Result/)).toHaveLength(2); | ||
}); | ||
|
||
it('Should return props from getServerSideProps', async () => { | ||
const expectedResult = [{ name: 'Test' }]; | ||
|
||
const client: HttpClient<'entur', AutocompleteApi> = { | ||
async autocomplete() { | ||
return expectedResult; | ||
}, | ||
async request() { | ||
return new Response(); | ||
}, | ||
}; | ||
const result = await getServerSidePropsWithClient( | ||
client, | ||
getServerSideProps, | ||
); | ||
|
||
(await expectProps(result)).toContain({ | ||
autocompleteFeatures: expectedResult, | ||
}); | ||
}); | ||
}); |
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,3 @@ | ||
import { vi } from 'vitest'; | ||
|
||
vi.stubEnv('NEXT_PUBLIC_WEBSHOP_ORG_ID', 'atb'); |
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,31 @@ | ||
import { HttpClient } from '@atb/modules/api-server'; | ||
import { HttpEndpoints } from '@atb/modules/api-server/utils'; | ||
import type { | ||
GetServerSideProps, | ||
GetServerSidePropsContext, | ||
GetServerSidePropsResult, | ||
} from 'next/types'; | ||
import { Assertion, expect } from 'vitest'; | ||
|
||
export async function expectProps<T>( | ||
potential: GetServerSidePropsResult<T>, | ||
): Promise<Assertion<Awaited<T>>> { | ||
if (!('props' in potential)) { | ||
expect.unreachable(); | ||
} | ||
|
||
const props = await potential.props; | ||
return expect(props); | ||
} | ||
|
||
export async function getServerSidePropsWithClient< | ||
U extends HttpEndpoints, | ||
M, | ||
T extends { [key: string]: any } = { [key: string]: any }, | ||
>( | ||
client: HttpClient<U, M>, | ||
propsHandler: GetServerSideProps<T>, | ||
context?: GetServerSidePropsContext<T>, | ||
) { | ||
return propsHandler({ ...context, client } as GetServerSidePropsContext<T>); | ||
} |
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 @@ | ||
/// <reference types="vitest" /> | ||
import { defineConfig } from 'vite'; | ||
import react from '@vitejs/plugin-react'; | ||
import path from 'path'; | ||
|
||
export default defineConfig({ | ||
plugins: [react()], | ||
|
||
test: { | ||
setupFiles: './src/tests/setup.ts', | ||
environment: 'happy-dom', | ||
}, | ||
resolve: { | ||
alias: { | ||
'@atb': path.resolve(__dirname, './src'), | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.