Skip to content

Commit

Permalink
test(next-drupal): reorganize Jest tests
Browse files Browse the repository at this point in the history
Fixes #608
  • Loading branch information
JohnAlbin committed Feb 20, 2024
1 parent dbb6e91 commit 709cd80
Show file tree
Hide file tree
Showing 29 changed files with 9,268 additions and 6,661 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{*.diff,*.patch}]
trim_trailing_whitespace = false
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@
"faker": "^6.6.6",
"husky": "^8.0.3",
"isomorphic-fetch": "^3.0.0",
"jest": "^27.5.1",
"jest": "^29.7.0",
"lerna": "^8.0.1",
"lint-staged": "^15.0.2",
"prettier": "^3.0.3",
"start-server-and-test": "^1.15.5",
"ts-jest": "^27.1.5",
"ts-node": "^10.9.1",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tsup": "^7.3.0",
"turbo": "^1.10.15",
"typescript": "^5.2.2",
Expand Down
16 changes: 10 additions & 6 deletions packages/next-drupal-query/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest/presets/js-with-ts",
testEnvironment: "node",
setupFiles: ["dotenv/config"],
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
testMatch: ["**/tests/**/*.test.{ts,tsx}"],
globals: {
"ts-jest": {
isolatedModules: true,
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
isolatedModules: true,
},
],
},
testLocationInResults: true,
coverageProvider: "v8",
collectCoverage: true,
collectCoverageFrom: ["./src/**"],
coverageReporters: ["lcov", "text", "text-summary"],
coverageThreshold: {
global: {
statements: 85.11,
branches: 58,
branches: 67.56,
functions: 70,
lines: 85.11,
},
Expand Down
42 changes: 21 additions & 21 deletions packages/next-drupal-query/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`formatData it formats data 1`] = `
Object {
{
"name": "First Last",
}
`;

exports[`getData it accepts data options 1`] = `
Object {
{
"author": "shadcn",
"id": "ID",
"title": "Title of Article",
Expand All @@ -16,14 +16,14 @@ Object {
`;

exports[`getData it accepts placeholder options 1`] = `
Object {
{
"email": "[email protected]",
"name": "shadcn",
}
`;

exports[`getData it returns data for a query 1`] = `
Object {
{
"author": "shadcn",
"id": "id",
"title": "Title of Article",
Expand All @@ -32,52 +32,52 @@ Object {
`;

exports[`getData it returns data when both data and placeholder is defined 1`] = `
Object {
{
"email": "[email protected]",
}
`;

exports[`getData it returns formatted data if a formatter is defined 1`] = `
Object {
{
"email": "[email protected]",
"name": "First Last",
}
`;

exports[`getData it returns placeholder data if a query does not define data 1`] = `
Object {
{
"name": "shadcn",
}
`;

exports[`getParams it accepts params options 1`] = `
Object {
"fields": Object {
{
"fields": {
"resource": "foo,bar",
},
"filter": Object {},
"include": Array [],
"sort": Array [],
"filter": {},
"include": [],
"sort": [],
}
`;

exports[`getParams it allows params to be nested 1`] = `
Object {
"fields": Object {},
"filter": Object {},
{
"fields": {},
"filter": {},
"include": "field_image,uid",
"page": Object {
"page": {
"limit": 10,
},
"sort": Array [],
"sort": [],
}
`;

exports[`getParams it returns params for a query 1`] = `
Object {
"fields": Object {},
"filter": Object {},
{
"fields": {},
"filter": {},
"include": "field_image,uid",
"sort": Array [],
"sort": [],
}
`;
22 changes: 13 additions & 9 deletions packages/next-drupal/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
setupFiles: ["dotenv/config"],
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
testMatch: ["**/tests/**/*.test.{ts,tsx}"],
globals: {
"ts-jest": {
isolatedModules: true,
},
transform: {
"^.+\\.tsx?$": [
"ts-jest",
{
isolatedModules: true,
},
],
},
testLocationInResults: true,
coverageProvider: "v8",
collectCoverage: true,
collectCoverageFrom: ["./src/**"],
Expand All @@ -22,10 +26,10 @@ module.exports = {
coverageReporters: ["lcov", "text", "text-summary"],
coverageThreshold: {
global: {
statements: 82.07,
branches: 86.9,
functions: 80.76,
lines: 82.07,
statements: 97.21,
branches: 92.25,
functions: 100,
lines: 97.21,
},
},
}
43 changes: 12 additions & 31 deletions packages/next-drupal/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

const json = await response.json()

Expand Down Expand Up @@ -383,9 +381,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

const json = await response.json()

Expand Down Expand Up @@ -423,9 +419,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

const json = await response.json()

Expand Down Expand Up @@ -457,9 +451,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

return response.status === 204
}
Expand Down Expand Up @@ -502,9 +494,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

const json = await response.json()

Expand Down Expand Up @@ -735,9 +725,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

const json = await response.json()

Expand Down Expand Up @@ -1073,6 +1061,7 @@ export class DrupalClient {
response: NextApiResponse,
options?: Parameters<NextApiResponse["setDraftMode"]>[0]
) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { slug, resourceVersion, plugin, secret, scope, ...draftData } =
request.query
const useDraftMode = options?.enable
Expand Down Expand Up @@ -1192,9 +1181,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

const data = await response.json()

Expand Down Expand Up @@ -1265,9 +1252,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

const data = await response.json()

Expand Down Expand Up @@ -1307,9 +1292,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

const json = await response.json()

Expand Down Expand Up @@ -1407,9 +1390,7 @@ export class DrupalClient {
body,
})

if (!response?.ok) {
await this.handleJsonApiErrors(response)
}
await this.throwIfJsonApiErrors(response)

const result: AccessToken = await response.json()

Expand Down Expand Up @@ -1476,7 +1457,7 @@ export class DrupalClient {
throw error
}

private async handleJsonApiErrors(response: Response) {
private async throwIfJsonApiErrors(response: Response) {
if (!response?.ok) {
const errors = await this.getErrorsFromResponse(response)
throw new JsonApiErrors(errors, response.status)
Expand Down
1 change: 1 addition & 0 deletions packages/next-drupal/src/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export async function enableDraftMode(
}

// Send Drupal's data to the draft-mode page.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { secret, scope, plugin, ...draftData } = Object.fromEntries(
searchParams.entries()
)
Expand Down
11 changes: 11 additions & 0 deletions packages/next-drupal/tests/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": ["../../../.eslintrc.json"],
"overrides": [
{
"files": ["*.test.ts"],
"rules": {
"@typescript-eslint/ban-ts-comment": "off"
}
}
]
}
Loading

0 comments on commit 709cd80

Please sign in to comment.