mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore(site): Add unit tests, mocks (#514)
* Extract and unit test redirect functions * Move router to make app more testable * Make mock entities more consistent * Use labels instead of placeholders * Fill out handlers * Lint * Reorganize App * Make mock entities reference each other * Add describes in tests * Clean up api and mocks
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { embedRedirect, retrieveRedirect } from "./redirect"
|
||||
|
||||
describe("redirect helper functions", () => {
|
||||
describe("embedRedirect", () => {
|
||||
it("embeds the page to return to in the URL", () => {
|
||||
const result = embedRedirect("/workspaces", "/page")
|
||||
expect(result).toEqual("/page?redirect=%2Fworkspaces")
|
||||
})
|
||||
it("defaults to navigating to the login page", () => {
|
||||
const result = embedRedirect("/workspaces")
|
||||
expect(result).toEqual("/login?redirect=%2Fworkspaces")
|
||||
})
|
||||
})
|
||||
describe("retrieveRedirect", () => {
|
||||
it("retrieves the page to return to from the URL", () => {
|
||||
const result = retrieveRedirect("?redirect=%2Fworkspaces")
|
||||
expect(result).toEqual("/workspaces")
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Creates a url containing a page to navigate to now, and embedding another
|
||||
* URL in the query string so you can return to it later.
|
||||
* @param navigateTo page to navigate to now (by default, /login)
|
||||
* @param returnTo page to redirect to later (for instance, after logging in)
|
||||
* @returns URL containing a redirect query parameter
|
||||
*/
|
||||
export const embedRedirect = (returnTo: string, navigateTo = "/login"): string =>
|
||||
`${navigateTo}?redirect=${encodeURIComponent(returnTo)}`
|
||||
|
||||
/**
|
||||
* Retrieves a url from the query string of the current URL
|
||||
* @param search the query string in the current URL
|
||||
* @returns the URL to redirect to
|
||||
*/
|
||||
export const retrieveRedirect = (search: string): string => {
|
||||
const defaultRedirect = "/"
|
||||
const searchParams = new URLSearchParams(search)
|
||||
const redirect = searchParams.get("redirect")
|
||||
return redirect ? redirect : defaultRedirect
|
||||
}
|
||||
Reference in New Issue
Block a user