-
Notifications
You must be signed in to change notification settings - Fork 40
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
1 parent
ba15a01
commit 530be7d
Showing
36 changed files
with
4,786 additions
and
707 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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,79 @@ | ||
import test from "ava"; | ||
import * as React from "react"; | ||
import browserEnv from "browser-env"; | ||
|
||
import { renderJSON } from "./__testutils__/renderJSON"; | ||
import { md5 } from "./__testutils__/md5"; | ||
|
||
import { PrismicToolbar } from "../src"; | ||
|
||
/** Retrieves the toolbar script for a specific repository. */ | ||
const getToolbarScript = (repositoryName: string): Element | null => { | ||
return document.body.querySelector( | ||
`[data-prismic-toolbar=""][data-repository-name="${repositoryName}"]`, | ||
); | ||
}; | ||
|
||
// Polyfill the runtime with a simulated DOM. | ||
test.before(() => { | ||
browserEnv(); | ||
}); | ||
|
||
test.serial( | ||
"adds a script element with the correct attributes to document.body", | ||
(t) => { | ||
const repositoryName = md5(t.title); | ||
|
||
renderJSON(<PrismicToolbar repositoryName={repositoryName} type="new" />); | ||
|
||
const script = getToolbarScript(repositoryName); | ||
|
||
if (script instanceof HTMLScriptElement) { | ||
t.is( | ||
script.getAttribute("src"), | ||
`https://static.cdn.prismic.io/prismic.js?repositoryName=${repositoryName}&type=new`, | ||
); | ||
t.is(script.getAttribute("defer"), ""); | ||
t.is(script.dataset.repositoryName, repositoryName); | ||
t.is(script.dataset.type, "new"); | ||
} else { | ||
t.fail("The toolbar script element was not found"); | ||
} | ||
}, | ||
); | ||
|
||
test.serial("uses the new toolbar by default", (t) => { | ||
const repositoryName = md5(t.title); | ||
|
||
renderJSON(<PrismicToolbar repositoryName={repositoryName} />); | ||
|
||
const script = getToolbarScript(repositoryName); | ||
|
||
if (script instanceof HTMLScriptElement) { | ||
t.is( | ||
script.getAttribute("src"), | ||
`https://static.cdn.prismic.io/prismic.js?repositoryName=${repositoryName}&type=new`, | ||
); | ||
t.is(script.dataset.type, "new"); | ||
} else { | ||
t.fail("The toolbar script element was not found"); | ||
} | ||
}); | ||
|
||
test('uses the legacy toolbar if type is set to "legacy"', (t) => { | ||
const repositoryName = md5(t.title); | ||
|
||
renderJSON(<PrismicToolbar repositoryName={repositoryName} type="legacy" />); | ||
|
||
const script = getToolbarScript(repositoryName); | ||
|
||
if (script instanceof HTMLScriptElement) { | ||
t.is( | ||
script.getAttribute("src"), | ||
`https://static.cdn.prismic.io/prismic.js?repositoryName=${repositoryName}`, | ||
); | ||
t.is(script.dataset.type, "legacy"); | ||
} else { | ||
t.fail("The toolbar script element was not found"); | ||
} | ||
}); |
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 * as ava from "ava"; | ||
import * as prismic from "@prismicio/client"; | ||
import fetch from "node-fetch"; | ||
|
||
import { md5 } from "./md5"; | ||
|
||
export const createClient = (t: ava.ExecutionContext): prismic.Client => { | ||
const endpoint = prismic.getEndpoint(md5(t.title)); | ||
const client = prismic.createClient(endpoint, { fetch }); | ||
|
||
return client; | ||
}; |
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,28 @@ | ||
import * as prismicT from "@prismicio/types"; | ||
|
||
export const createDocument = < | ||
TDocument extends prismicT.PrismicDocument = prismicT.PrismicDocument, | ||
>( | ||
fields?: Partial<TDocument>, | ||
): TDocument => { | ||
const id = Math.random().toString(); | ||
const uid = Math.random().toString(); | ||
|
||
return { | ||
id, | ||
uid, | ||
type: "type", | ||
href: "href", | ||
tags: ["tag"], | ||
slugs: ["slug"], | ||
lang: "lang", | ||
alternate_languages: [], | ||
first_publication_date: "first_publication_date", | ||
last_publication_date: "last_publication_date", | ||
linked_documents: [], | ||
...fields, | ||
data: { | ||
...fields?.data, | ||
}, | ||
} as TDocument; | ||
}; |
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,79 @@ | ||
import * as ava from "ava"; | ||
import * as msw from "msw"; | ||
import * as prismic from "@prismicio/client"; | ||
import * as prismicT from "@prismicio/types"; | ||
|
||
import { createQueryResponse } from "./createQueryResponse"; | ||
import { md5 } from "./md5"; | ||
|
||
const castArray = <A>(a: A | A[]): A[] => (Array.isArray(a) ? a : [a]); | ||
|
||
export const createMockQueryHandler = < | ||
TDocument extends prismicT.PrismicDocument = prismicT.PrismicDocument, | ||
>( | ||
t: ava.ExecutionContext, | ||
pagedResponses: Partial<prismic.Query<TDocument>>[] = [createQueryResponse()], | ||
requiredSearchParams?: Record< | ||
string, | ||
string | number | (string | number)[] | undefined | ||
>, | ||
debug = true, | ||
): msw.RestHandler => { | ||
const repositoryName = md5(t.title); | ||
const endpoint = `https://${repositoryName}.cdn.prismic.io/api/v2/documents/search`; | ||
|
||
return msw.rest.get(endpoint, (req, res, ctx) => { | ||
const page = Number.parseInt(req.url.searchParams.get("page") ?? "1"); | ||
|
||
let requestMatches = true; | ||
|
||
if (requiredSearchParams) { | ||
const requiredSearchParamsInstance = new URLSearchParams(); | ||
for (const k in requiredSearchParams) { | ||
castArray( | ||
requiredSearchParams[k as keyof typeof requiredSearchParams], | ||
).forEach( | ||
(l) => | ||
l !== undefined && | ||
requiredSearchParamsInstance.append(k, l.toString()), | ||
); | ||
} | ||
|
||
if (!("page" in requiredSearchParams) && page > 1) { | ||
requiredSearchParamsInstance.append("page", page.toString()); | ||
} | ||
|
||
// TODO: Remove when the Authorization header can be used | ||
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351} | ||
const searchParamsWithoutAccessToken = new URLSearchParams( | ||
req.url.searchParams, | ||
); | ||
searchParamsWithoutAccessToken.delete("access_token"); | ||
|
||
if (debug) { | ||
t.is( | ||
requiredSearchParamsInstance.toString(), | ||
// TODO: Uncomment when the Authorization header can be used | ||
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351} | ||
// req.url.searchParams.toString() | ||
searchParamsWithoutAccessToken.toString(), | ||
); | ||
} | ||
|
||
requestMatches = | ||
requiredSearchParamsInstance.toString() === | ||
// TODO: Uncomment when the Authorization header can be used | ||
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351} | ||
// req.url.searchParams.toString() | ||
searchParamsWithoutAccessToken.toString(); | ||
} | ||
|
||
if (requestMatches) { | ||
const response = pagedResponses[page - 1]; | ||
|
||
return res(ctx.json(response)); | ||
} | ||
|
||
return res(ctx.status(404)); | ||
}); | ||
}; |
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 * as ava from "ava"; | ||
import * as msw from "msw"; | ||
import * as prismic from "@prismicio/client"; | ||
|
||
import { createRepositoryResponse } from "./createRepositoryResponse"; | ||
import { md5 } from "./md5"; | ||
|
||
export const createMockRepositoryHandler = ( | ||
t: ava.ExecutionContext, | ||
response = createRepositoryResponse(), | ||
): msw.RestHandler => { | ||
const repositoryName = md5(t.title); | ||
const endpoint = prismic.getEndpoint(repositoryName); | ||
|
||
return msw.rest.get(endpoint, (_req, res, ctx) => { | ||
return res(ctx.json(response)); | ||
}); | ||
}; |
Oops, something went wrong.