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 21, 2024
1 parent 109213e commit 50642c5
Show file tree
Hide file tree
Showing 26 changed files with 7,619 additions and 4,754 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
9 changes: 5 additions & 4 deletions packages/next-drupal/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
},
],
},
testLocationInResults: true,
coverageProvider: "v8",
collectCoverage: true,
collectCoverageFrom: ["./src/**"],
Expand All @@ -25,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: 100,
branches: 100,
functions: 100,
lines: 100,
},
},
}
83 changes: 41 additions & 42 deletions packages/next-drupal/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class DrupalClient {
if (token) {
init["headers"]["Authorization"] = `Bearer ${token.access_token}`
}
} else if (isAccessTokenAuth(this._auth)) {
} /* c8 ignore next 4 */ else if (isAccessTokenAuth(this._auth)) {
init["headers"]["Authorization"] =
`${this._auth.token_type} ${this._auth.access_token}`
}
Expand All @@ -273,7 +273,7 @@ export class DrupalClient {
this.debug(`Using custom authorization header.`)

init["headers"]["Authorization"] = init.withAuth
} else if (typeof init.withAuth === "function") {
} /* c8 ignore next 4 */ else if (typeof init.withAuth === "function") {
this.debug(`Using custom authorization callback.`)

init["headers"]["Authorization"] = init.withAuth()
Expand All @@ -292,7 +292,7 @@ export class DrupalClient {
if (token) {
init["headers"]["Authorization"] = `Bearer ${token.access_token}`
}
} else if (isAccessTokenAuth(init.withAuth)) {
} /* c8 ignore next 4 */ else if (isAccessTokenAuth(init.withAuth)) {
init["headers"]["Authorization"] =
`${init.withAuth.token_type} ${init.withAuth.access_token}`
}
Expand Down Expand Up @@ -322,7 +322,9 @@ export class DrupalClient {

const apiPath = await this.getEntryForResourceType(
type,
options?.locale !== options?.defaultLocale ? options.locale : undefined
options?.locale !== options?.defaultLocale
? /* c8 ignore next */ options.locale
: undefined
)

const url = this.buildUrl(apiPath, options?.params)
Expand All @@ -338,13 +340,13 @@ export class DrupalClient {
withAuth: options.withAuth,
})

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

const json = await response.json()

return options.deserialize ? this.deserialize(json) : json
return options.deserialize
? this.deserialize(json)
: /* c8 ignore next */ json
}

async createFileResource<T = DrupalFile>(
Expand Down Expand Up @@ -383,9 +385,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

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

const json = await response.json()

Expand All @@ -406,7 +406,9 @@ export class DrupalClient {

const apiPath = await this.getEntryForResourceType(
type,
options?.locale !== options?.defaultLocale ? options.locale : undefined
options?.locale !== options?.defaultLocale
? /* c8 ignore next */ options.locale
: undefined
)

const url = this.buildUrl(`${apiPath}/${uuid}`, options?.params)
Expand All @@ -423,13 +425,13 @@ export class DrupalClient {
withAuth: options.withAuth,
})

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

const json = await response.json()

return options.deserialize ? this.deserialize(json) : json
return options.deserialize
? this.deserialize(json)
: /* c8 ignore next */ json
}

async deleteResource(
Expand All @@ -445,7 +447,9 @@ export class DrupalClient {

const apiPath = await this.getEntryForResourceType(
type,
options?.locale !== options?.defaultLocale ? options.locale : undefined
options?.locale !== options?.defaultLocale
? /* c8 ignore next */ options.locale
: undefined
)

const url = this.buildUrl(`${apiPath}/${uuid}`, options?.params)
Expand All @@ -457,9 +461,7 @@ export class DrupalClient {
withAuth: options.withAuth,
})

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

return response.status === 204
}
Expand All @@ -477,6 +479,7 @@ export class DrupalClient {
...options,
}

/* c8 ignore next 11 */
if (options.withCache) {
const cached = (await this.cache.get(options.cacheKey)) as string

Expand All @@ -502,12 +505,11 @@ export class DrupalClient {
withAuth: options.withAuth,
})

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

const json = await response.json()

/* c8 ignore next 3 */
if (options.withCache) {
await this.cache.set(options.cacheKey, JSON.stringify(json))
}
Expand Down Expand Up @@ -568,6 +570,7 @@ export class DrupalClient {
// When we try to translate /es/example, decoupled router will properly
// translate to the untranslated version and set the locale to es.
// However a subrequests to /es/subrequests for decoupled router will fail.
/* c8 ignore next 3 */
if (context.locale && input.entity.langcode !== context.locale) {
context.locale = input.entity.langcode
}
Expand Down Expand Up @@ -621,7 +624,7 @@ export class DrupalClient {
options.defaultLocale &&
path.indexOf(options.locale) !== 1
) {
path = path === "/" ? path : path.replace(/^\/+/, "")
path = path === "/" ? /* c8 ignore next */ path : path.replace(/^\/+/, "")
path = this.getPathFromContext({
params: { slug: [path] },
locale: options.locale,
Expand Down Expand Up @@ -735,9 +738,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 @@ -1028,6 +1029,7 @@ export class DrupalClient {
const pattern = `^\\/${locale}\\/`
const path = href.replace(this.baseUrl, "")

/* c8 ignore next 3 */
if (!new RegExp(pattern, "i").test(path)) {
return `${this.baseUrl}/${locale}${path}`
}
Expand Down Expand Up @@ -1073,6 +1075,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 @@ -1167,6 +1170,7 @@ export class DrupalClient {
...options,
}

/* c8 ignore next 9 */
if (options.withCache) {
const cached = (await this.cache.get(options.cacheKey)) as string

Expand All @@ -1192,13 +1196,13 @@ export class DrupalClient {
withAuth: options.withAuth,
})

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

const data = await response.json()

const items = options.deserialize ? this.deserialize(data) : data
const items = options.deserialize
? this.deserialize(data)
: /* c8 ignore next */ data

const { items: tree } = this.buildMenuTree(items)

Expand All @@ -1207,6 +1211,7 @@ export class DrupalClient {
tree,
}

/* c8 ignore next 3 */
if (options.withCache) {
await this.cache.set(options.cacheKey, JSON.stringify(menu))
}
Expand Down Expand Up @@ -1265,9 +1270,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 +1310,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 @@ -1374,7 +1375,7 @@ export class DrupalClient {

const clientId = opts?.clientId || this._auth.clientId
const clientSecret = opts?.clientSecret || this._auth.clientSecret
const url = this.buildUrl(opts?.url || this._auth.url || DEFAULT_AUTH_URL)
const url = this.buildUrl(opts?.url || this._auth.url)

if (
this.accessTokenScope === opts?.scope &&
Expand Down Expand Up @@ -1407,9 +1408,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 +1475,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
3 changes: 2 additions & 1 deletion packages/next-drupal/src/types/resource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import type { JsonApiError, JsonApiLinks } from "../jsonapi-errors"
import type { PathAlias } from "./drupal"

Expand Down Expand Up @@ -53,7 +55,6 @@ export interface JsonApiUpdateResourceBody {
}
}

/* eslint-disable @typescript-eslint/no-explicit-any */
export interface JsonApiResource extends Record<string, any> {
id: string
type: string
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 50642c5

Please sign in to comment.