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:
Presley Pizzo
2022-03-23 10:09:43 -04:00
committed by GitHub
parent 3bf5ceb600
commit f2ac81cc2e
10 changed files with 167 additions and 124 deletions
+20
View File
@@ -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")
})
})
})
+21
View File
@@ -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
}