-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Showing
19 changed files
with
369 additions
and
28 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 |
---|---|---|
|
@@ -59,3 +59,6 @@ app/yarn.lock | |
|
||
# Prisma migrations | ||
/prisma/migrations | ||
|
||
# Tests | ||
/coverage |
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,8 +1,11 @@ | ||
/** @type {import('@jest/types').Config.InitialOptions} */ | ||
module.exports = { | ||
transform: { | ||
"\\.js$": ["babel-jest", { configFile: "./config/babel.config.js" }], | ||
}, | ||
roots: ["../src"], | ||
setupFilesAfterEnv: ["./jest-setup.js"], | ||
rootDir: "../src", | ||
setupFilesAfterEnv: ["../config/jest-setup.js"], | ||
collectCoverageFrom: ["!client/__tests__/**"], | ||
testMatch: ["**/*.test.js"], | ||
coverageDirectory: "../coverage", | ||
} |
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,64 @@ | ||
import { useState } from "react" | ||
import { rest } from "msw" | ||
import { render, screen, waitFor } from "@testing-library/react" | ||
import { server, mockSession } from "./helpers/mocks" | ||
import { Provider, useSession } from ".." | ||
import userEvent from "@testing-library/user-event" | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
server.resetHandlers() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
test("fetches the session once and re-uses it for different consumers", async () => { | ||
const sessionRouteCall = jest.fn() | ||
|
||
server.use( | ||
rest.get("/api/auth/session", (req, res, ctx) => { | ||
sessionRouteCall() | ||
res(ctx.status(200), ctx.json(mockSession)) | ||
}) | ||
) | ||
|
||
render(<ProviderFlow />) | ||
|
||
await waitFor(() => { | ||
expect(sessionRouteCall).toHaveBeenCalledTimes(1) | ||
|
||
const session1 = screen.getByTestId("session-consumer-1").textContent | ||
const session2 = screen.getByTestId("session-consumer-2").textContent | ||
|
||
expect(session1).toEqual(session2) | ||
}) | ||
}) | ||
|
||
function ProviderFlow({ options = {} }) { | ||
return ( | ||
<> | ||
<Provider options={options}> | ||
<SessionConsumer /> | ||
<SessionConsumer testId="2" /> | ||
</Provider> | ||
</> | ||
) | ||
} | ||
|
||
function SessionConsumer({ testId = 1 }) { | ||
const [session, loading] = useSession() | ||
|
||
if (loading) return <span>loading</span> | ||
|
||
return ( | ||
<div data-testid={`session-consumer-${testId}`}> | ||
{JSON.stringify(session)} | ||
</div> | ||
) | ||
} |
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,105 @@ | ||
import { useState } from "react" | ||
import userEvent from "@testing-library/user-event" | ||
import { render, screen, waitFor } from "@testing-library/react" | ||
import { server, mockCSRFToken } from "./helpers/mocks" | ||
import logger from "../../lib/logger" | ||
import { getCsrfToken } from ".." | ||
import { rest } from "msw" | ||
|
||
jest.mock("../../lib/logger", () => ({ | ||
__esModule: true, | ||
default: { | ||
warn: jest.fn(), | ||
debug: jest.fn(), | ||
error: jest.fn(), | ||
}, | ||
proxyLogger(logger) { | ||
return logger | ||
}, | ||
})) | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
test("returns the Cross Site Request Forgery Token (CSRF Token) required to make POST requests", async () => { | ||
render(<CSRFFlow />) | ||
|
||
userEvent.click(screen.getByRole("button")) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByTestId("csrf-result").textContent).toEqual( | ||
mockCSRFToken.csrfToken | ||
) | ||
}) | ||
}) | ||
|
||
test("when there's no CSRF token returned, it'll reflect that", async () => { | ||
server.use( | ||
rest.get("/api/auth/csrf", (req, res, ctx) => | ||
res( | ||
ctx.status(200), | ||
ctx.json({ | ||
...mockCSRFToken, | ||
csrfToken: null, | ||
}) | ||
) | ||
) | ||
) | ||
|
||
render(<CSRFFlow />) | ||
|
||
userEvent.click(screen.getByRole("button")) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByTestId("csrf-result").textContent).toBe("null-response") | ||
}) | ||
}) | ||
|
||
test("when the fetch fails it'll throw a client fetch error", async () => { | ||
server.use( | ||
rest.get("/api/auth/csrf", (req, res, ctx) => | ||
res(ctx.status(500), ctx.text("some error happened")) | ||
) | ||
) | ||
|
||
render(<CSRFFlow />) | ||
|
||
userEvent.click(screen.getByRole("button")) | ||
|
||
await waitFor(() => { | ||
expect(logger.error).toHaveBeenCalledTimes(1) | ||
expect(logger.error).toBeCalledWith( | ||
"CLIENT_FETCH_ERROR", | ||
"csrf", | ||
new SyntaxError("Unexpected token s in JSON at position 0") | ||
) | ||
}) | ||
}) | ||
|
||
function CSRFFlow() { | ||
const [response, setResponse] = useState() | ||
|
||
async function handleCSRF() { | ||
const result = await getCsrfToken() | ||
setResponse(result) | ||
} | ||
|
||
return ( | ||
<> | ||
<p data-testid="csrf-result"> | ||
{response === null ? "null-response" : response || "no response"} | ||
</p> | ||
<button onClick={handleCSRF}>Get CSRF</button> | ||
</> | ||
) | ||
} |
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
File renamed without changes.
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,85 @@ | ||
import { useState } from "react" | ||
import userEvent from "@testing-library/user-event" | ||
import { render, screen, waitFor } from "@testing-library/react" | ||
import { server, mockProviders } from "./helpers/mocks" | ||
import { getProviders } from ".." | ||
import logger from "../../lib/logger" | ||
import { rest } from "msw" | ||
|
||
jest.mock("../../lib/logger", () => ({ | ||
__esModule: true, | ||
default: { | ||
warn: jest.fn(), | ||
debug: jest.fn(), | ||
error: jest.fn(), | ||
}, | ||
proxyLogger(logger) { | ||
return logger | ||
}, | ||
})) | ||
|
||
beforeAll(() => { | ||
server.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
server.resetHandlers() | ||
jest.clearAllMocks() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
}) | ||
|
||
test("when called it'll return the currently configured providers for sign in", async () => { | ||
render(<ProvidersFlow />) | ||
|
||
userEvent.click(screen.getByRole("button")) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByTestId("providers-result").textContent).toEqual( | ||
JSON.stringify(mockProviders) | ||
) | ||
}) | ||
}) | ||
|
||
test("when failing to fetch the providers, it'll log the error", async () => { | ||
server.use( | ||
rest.get("/api/auth/providers", (req, res, ctx) => | ||
res(ctx.status(500), ctx.text("some error happened")) | ||
) | ||
) | ||
|
||
render(<ProvidersFlow />) | ||
|
||
userEvent.click(screen.getByRole("button")) | ||
|
||
await waitFor(() => { | ||
expect(logger.error).toHaveBeenCalledTimes(1) | ||
expect(logger.error).toBeCalledWith( | ||
"CLIENT_FETCH_ERROR", | ||
"providers", | ||
new SyntaxError("Unexpected token s in JSON at position 0") | ||
) | ||
}) | ||
}) | ||
|
||
function ProvidersFlow() { | ||
const [response, setResponse] = useState() | ||
|
||
async function handleGerProviders() { | ||
const result = await getProviders() | ||
setResponse(result) | ||
} | ||
|
||
return ( | ||
<> | ||
<p data-testid="providers-result"> | ||
{response === null | ||
? "null-response" | ||
: JSON.stringify(response) || "no response"} | ||
</p> | ||
<button onClick={handleGerProviders}>Get Providers</button> | ||
</> | ||
) | ||
} |
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
Oops, something went wrong.