-
Notifications
You must be signed in to change notification settings - Fork 5
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
1e29322
commit 8e0ef1b
Showing
36 changed files
with
1,148 additions
and
256 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,17 @@ | |
}, | ||
"license": "Apache-2.0", | ||
"author": "Prismic <[email protected]> (https://prismic.io)", | ||
"sideEffects": false, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.mjs" | ||
}, | ||
"./api": { | ||
"require": "./dist/api/index.cjs", | ||
"import": "./dist/api/index.mjs" | ||
}, | ||
"./model": { | ||
"require": "./dist/model/index.cjs", | ||
"import": "./dist/model/index.mjs" | ||
|
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,11 @@ | ||
export { repository } from "./repository"; | ||
export type { MockRestApiRepositoryConfig } from "./repository"; | ||
|
||
export { ref } from "./ref"; | ||
export type { MockRestApiRefConfig, MockRestApiRefValue } from "./ref"; | ||
|
||
export { query } from "./query"; | ||
export type { MockRestApiQueryConfig } from "./query"; | ||
|
||
export { tags } from "./tags"; | ||
export type { MockRestApiTagsConfig } from "./tags"; |
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,38 @@ | ||
import * as prismicT from "@prismicio/types"; | ||
|
||
import { createFaker } from "../lib/createFaker"; | ||
|
||
import { MockRestApiConfig } from "../types"; | ||
|
||
export type MockRestApiQueryConfig< | ||
Document extends prismicT.PrismicDocument = prismicT.PrismicDocument, | ||
> = { | ||
documents?: Document[]; | ||
page?: number; | ||
pageSize?: number; | ||
} & MockRestApiConfig; | ||
|
||
export const query = < | ||
Document extends prismicT.PrismicDocument = prismicT.PrismicDocument, | ||
>( | ||
config: MockRestApiQueryConfig<Document> = {}, | ||
): prismicT.Query<Document> => { | ||
const faker = createFaker(config.seed); | ||
|
||
const documents = config.documents || []; | ||
const page = Math.max(1, config.page ?? 1); | ||
const pageSize = Math.min(100, Math.max(1, config.pageSize ?? 100)); | ||
const totalPages = Math.ceil(documents.length / pageSize); | ||
const results = documents.slice((page - 1) * pageSize, page * pageSize); | ||
|
||
return { | ||
page, | ||
next_page: page < totalPages ? faker.internet.url() : null, | ||
prev_page: page > 1 ? faker.internet.url() : null, | ||
total_pages: totalPages, | ||
results_size: results.length, | ||
results_per_page: pageSize, | ||
total_results_size: documents.length, | ||
results, | ||
}; | ||
}; |
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,41 @@ | ||
import * as prismicT from "@prismicio/types"; | ||
import * as changeCase from "change-case"; | ||
|
||
import { createFaker } from "../lib/createFaker"; | ||
import { MockRestApiConfig } from "../types"; | ||
import { timestamp } from "../value"; | ||
|
||
export type MockRestApiRefConfig<IsScheduled extends boolean = false> = { | ||
isMasterRef?: boolean; | ||
isScheduled?: IsScheduled; | ||
} & MockRestApiConfig; | ||
|
||
export type MockRestApiRefValue<IsScheduled extends boolean = false> = Omit< | ||
prismicT.Ref, | ||
"scheduledAt" | ||
> & | ||
(IsScheduled extends true | ||
? { scheduledAt: string } | ||
: { scheduledAt?: never }); | ||
|
||
export const ref = <IsScheduled extends boolean = false>( | ||
config: MockRestApiRefConfig<IsScheduled> = {}, | ||
): MockRestApiRefValue<IsScheduled> => { | ||
const faker = createFaker(config.seed); | ||
|
||
const value: prismicT.Ref = { | ||
id: faker.git.shortSha(), | ||
ref: faker.git.shortSha(), | ||
isMasterRef: config.isMasterRef ?? false, | ||
label: config.isMasterRef | ||
? "Master" | ||
: changeCase.capitalCase(faker.company.bsNoun()), | ||
}; | ||
|
||
if (config.isScheduled) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
value.scheduledAt = timestamp({ seed: config.seed })!; | ||
} | ||
|
||
return value as MockRestApiRefValue<IsScheduled>; | ||
}; |
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,54 @@ | ||
import * as prismicT from "@prismicio/types"; | ||
import * as changeCase from "change-case"; | ||
|
||
import { createFaker } from "../lib/createFaker"; | ||
import { generateTags } from "../lib/generateTags"; | ||
|
||
import { MockRestApiConfig } from "../types"; | ||
import { ref } from "./ref"; | ||
|
||
export type MockRestApiRepositoryConfig = { | ||
customTypeModels?: prismicT.CustomTypeModel[]; | ||
withReleases?: boolean; | ||
} & MockRestApiConfig; | ||
|
||
export const repository = ( | ||
config: MockRestApiRepositoryConfig = {}, | ||
): prismicT.Repository => { | ||
const faker = createFaker(config.seed); | ||
|
||
const types = (config.customTypeModels || []).reduce((acc, model) => { | ||
acc[model.id] = model.label; | ||
|
||
return acc; | ||
}, {} as prismicT.Repository["types"]); | ||
|
||
return { | ||
refs: [ | ||
ref({ seed: config.seed, isMasterRef: true }), | ||
...(config.withReleases | ||
? [ref({ seed: config.seed }), ref({ seed: config.seed })] | ||
: []), | ||
], | ||
integrationFieldsRef: ref({ seed: config.seed }).ref, | ||
types, | ||
languages: [ | ||
{ | ||
id: faker.lorem.word(), | ||
name: changeCase.capitalCase(faker.lorem.word()), | ||
}, | ||
], | ||
tags: generateTags({ | ||
seed: config.seed, | ||
min: 1, | ||
max: 10, | ||
}), | ||
forms: {}, | ||
license: "All Rights Reserved", | ||
version: faker.git.shortSha(), | ||
bookmarks: {}, | ||
experiments: {}, | ||
oauth_token: faker.internet.url(), | ||
oauth_initiate: faker.internet.url(), | ||
}; | ||
}; |
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,15 @@ | ||
import * as prismicT from "@prismicio/types"; | ||
|
||
import { generateTags } from "../lib/generateTags"; | ||
|
||
import { MockRestApiConfig } from "../types"; | ||
|
||
export type MockRestApiTagsConfig = MockRestApiConfig; | ||
|
||
export const tags = (config: MockRestApiTagsConfig = {}): prismicT.Tags => { | ||
return generateTags({ | ||
seed: config.seed, | ||
min: 1, | ||
max: 10, | ||
}); | ||
}; |
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,2 +1,3 @@ | ||
export * as api from "./api"; | ||
export * as model from "./model"; | ||
export * as value from "./value"; |
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,45 @@ | ||
import test from "ava"; | ||
|
||
import { snapshotTwiceMacro } from "./__testutils__/snapshotTwiceMacro"; | ||
|
||
import * as mock from "../src"; | ||
|
||
test("creates a mock query value", snapshotTwiceMacro, mock.api.query); | ||
|
||
test("supports custom seed", snapshotTwiceMacro, () => | ||
mock.api.query({ seed: 1 }), | ||
); | ||
|
||
test("can be configured to return a set of documents", (t) => { | ||
const documents = Array(20) | ||
.fill(undefined) | ||
.map(() => mock.value.document()); | ||
|
||
const actual = mock.api.query({ documents }); | ||
|
||
t.deepEqual(actual.results, documents); | ||
}); | ||
|
||
test("can be configured to return paginated results", (t) => { | ||
const documents = Array(100) | ||
.fill(undefined) | ||
.map(() => mock.value.document()); | ||
const page = 2; | ||
const pageSize = 10; | ||
|
||
const actual = mock.api.query({ | ||
documents, | ||
page: 2, | ||
pageSize: 10, | ||
}); | ||
|
||
t.is(actual.results.length, pageSize); | ||
t.is(actual.page, page); | ||
t.is(actual.total_pages, Math.ceil(documents.length / pageSize)); | ||
t.is(typeof actual.next_page, "string"); | ||
t.is(typeof actual.prev_page, "string"); | ||
t.is(actual.results_size, pageSize); | ||
t.is(actual.results_per_page, pageSize); | ||
|
||
t.deepEqual(actual.results, documents.slice(10, 20)); | ||
}); |
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,24 @@ | ||
import test from "ava"; | ||
|
||
import { snapshotTwiceMacro } from "./__testutils__/snapshotTwiceMacro"; | ||
|
||
import * as mock from "../src"; | ||
|
||
test("creates a mock ref value", snapshotTwiceMacro, mock.api.ref); | ||
|
||
test("supports custom seed", snapshotTwiceMacro, () => | ||
mock.api.ref({ seed: 1 }), | ||
); | ||
|
||
test("can be configured to return a master ref", (t) => { | ||
const actual = mock.api.ref({ isMasterRef: true }); | ||
|
||
t.is(actual.isMasterRef, true); | ||
t.is(actual.label, "Master"); | ||
}); | ||
|
||
test("can be configured to return a scheduled ref", (t) => { | ||
const actual = mock.api.ref({ isScheduled: true }); | ||
|
||
t.is(typeof actual.scheduledAt, "string"); | ||
}); |
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,32 @@ | ||
import test from "ava"; | ||
|
||
import { snapshotTwiceMacro } from "./__testutils__/snapshotTwiceMacro"; | ||
|
||
import * as mock from "../src"; | ||
|
||
test( | ||
"creates a mock repository value", | ||
snapshotTwiceMacro, | ||
mock.api.repository, | ||
); | ||
|
||
test("supports custom seed", snapshotTwiceMacro, () => | ||
mock.api.repository({ seed: 1 }), | ||
); | ||
|
||
test("can be configured to include releases", (t) => { | ||
const actual = mock.api.repository({ withReleases: true }); | ||
|
||
t.true(actual.refs.filter((ref) => !ref.isMasterRef).length > 0); | ||
}); | ||
|
||
test("can be configured to include custom types", (t) => { | ||
const customTypeModels = [mock.model.customType(), mock.model.customType()]; | ||
|
||
const actual = mock.api.repository({ customTypeModels }); | ||
|
||
t.deepEqual(actual.types, { | ||
[customTypeModels[0].id]: customTypeModels[0].label, | ||
[customTypeModels[1].id]: customTypeModels[1].label, | ||
}); | ||
}); |
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,11 @@ | ||
import test from "ava"; | ||
|
||
import { snapshotTwiceMacro } from "./__testutils__/snapshotTwiceMacro"; | ||
|
||
import * as mock from "../src"; | ||
|
||
test("creates a mock tags value", snapshotTwiceMacro, mock.api.tags); | ||
|
||
test("supports custom seed", snapshotTwiceMacro, () => | ||
mock.api.tags({ seed: 1 }), | ||
); |
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,24 @@ | ||
import test from "ava"; | ||
|
||
import { snapshotTwiceMacro } from "./__testutils__/snapshotTwiceMacro"; | ||
|
||
import * as mock from "../src"; | ||
|
||
test("creates a mock ref value", snapshotTwiceMacro, mock.api.ref); | ||
|
||
test("supports custom seed", snapshotTwiceMacro, () => | ||
mock.api.ref({ seed: 1 }), | ||
); | ||
|
||
test("can be configured to return a master ref", (t) => { | ||
const actual = mock.api.ref({ isMasterRef: true }); | ||
|
||
t.is(actual.isMasterRef, true); | ||
t.is(actual.label, "Master"); | ||
}); | ||
|
||
test("can be configured to return a scheduled ref", (t) => { | ||
const actual = mock.api.ref({ isScheduled: true }); | ||
|
||
t.is(typeof actual.scheduledAt, "string"); | ||
}); |
Oops, something went wrong.