Skip to content

Commit

Permalink
Use @fetch-mock/jest
Browse files Browse the repository at this point in the history
And remove fetch overload from classes.
  • Loading branch information
j0k3r committed Oct 25, 2024
1 parent 6167624 commit 61071d1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 70 deletions.
4 changes: 2 additions & 2 deletions functions/classes/ExtensionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import parse from 'diffparser'
import { Handler } from './Handler'

export class ExtensionHandler extends Handler {
constructor(githubToken, namespace, fetch = null) {
super(githubToken, fetch)
constructor(githubToken, namespace) {
super(githubToken)

this.namespace = namespace
}
Expand Down
8 changes: 1 addition & 7 deletions functions/classes/Handler.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { Octokit } from '@octokit/rest'

export class Handler {
constructor(githubToken, fetch = null) {
constructor(githubToken) {
const options = {
auth: githubToken,
}

if (fetch) {
options.request = {
fetch,
}
}

this.githubClient = new Octokit(options)
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@babel/core": "^7.25.9",
"@babel/eslint-parser": "^7.25.9",
"@babel/preset-env": "^7.25.9",
"@fetch-mock/jest": "^0.2.0",
"babel-loader": "^9.2.1",
"babel-plugin-source-map-support": "^2.2.0",
"eslint": "^8.57.1",
Expand All @@ -22,7 +23,6 @@
"eslint-plugin-jsx-a11y": "^6.10.1",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-react": "^7.37.2",
"fetch-mock": "^12.0.0",
"jest": "^29.7.0",
"nock": "^13.5.4",
"node-fetch": "2",
Expand Down
102 changes: 49 additions & 53 deletions tests/extension.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nock from 'nock'
import fetchMock from 'fetch-mock'
import fetchMock from '@fetch-mock/jest'
import { ExtensionHandler } from '../functions/classes/ExtensionHandler'
import { handler } from '../functions/extension'

Expand Down Expand Up @@ -124,12 +124,7 @@ describe('Validating extension', () => {
})

test('file extension is not txt', async () => {
const mock = fetchMock
.sandbox()
.mock(
'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511',
200
)
fetchMock.mockGlobal().route('*', 200)

nock('http://git.hub').get('/diff').replyWithFile(200, `${__dirname}/fixtures/no_txt.diff`)

Expand All @@ -151,7 +146,7 @@ describe('Validating extension', () => {
},
}

const extension = new ExtensionHandler('GH_TOKEN', 'Test config', mock)
const extension = new ExtensionHandler('GH_TOKEN', 'Test config')
await extension.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -160,21 +155,20 @@ describe('Validating extension', () => {
statusCode: 204,
})

const lastOptions = JSON.parse(mock.lastOptions().body)
expect(lastOptions).toStrictEqual({
context: 'Test config - File extension check',
description: 'Fail: "theatlantic.com" has not a txt extension',
state: 'failure',
})
expect(fetch).toHaveFetched(
'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511',
{
body: {
context: 'Test config - File extension check',
description: 'Fail: "theatlantic.com" has not a txt extension',
state: 'failure',
},
}
)
})

test('at least one file extension is not txt', async () => {
const mock = fetchMock
.sandbox()
.mock(
'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511',
200
)
fetchMock.mockGlobal().route('*', 200)

nock('http://git.hub')
.get('/diff')
Expand All @@ -198,7 +192,7 @@ describe('Validating extension', () => {
},
}

const extension = new ExtensionHandler('GH_TOKEN', 'Test config', mock)
const extension = new ExtensionHandler('GH_TOKEN', 'Test config')
await extension.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -207,21 +201,20 @@ describe('Validating extension', () => {
statusCode: 204,
})

const lastOptions = JSON.parse(mock.lastOptions().body)
expect(lastOptions).toStrictEqual({
context: 'Test config - File extension check',
description: 'Fail: "wow.gamona.de" has not a txt extension',
state: 'failure',
})
expect(fetch).toHaveFetched(
'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511',
{
body: {
context: 'Test config - File extension check',
description: 'Fail: "wow.gamona.de" has not a txt extension',
state: 'failure',
},
}
)
})

test('one deleted file', async () => {
const mock = fetchMock
.sandbox()
.mock(
'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511',
200
)
fetchMock.mockGlobal().route('*', 200)

nock('http://git.hub')
.get('/diff')
Expand All @@ -245,7 +238,7 @@ describe('Validating extension', () => {
},
}

const extension = new ExtensionHandler('GH_TOKEN', 'Test config', mock)
const extension = new ExtensionHandler('GH_TOKEN', 'Test config')
await extension.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -254,21 +247,20 @@ describe('Validating extension', () => {
statusCode: 204,
})

const lastOptions = JSON.parse(mock.lastOptions().body)
expect(lastOptions).toStrictEqual({
context: 'Test config - File extension check',
description: 'passed',
state: 'success',
})
expect(fetch).toHaveFetched(
'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511',
{
body: {
context: 'Test config - File extension check',
description: 'passed',
state: 'success',
},
}
)
})

test('file extension is ok', async () => {
const mock = fetchMock
.sandbox()
.mock(
'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511',
200
)
fetchMock.mockGlobal().route('*', 200)

nock('http://git.hub').get('/diff').replyWithFile(200, `${__dirname}/fixtures/with_a.txt.diff`)

Expand All @@ -290,7 +282,7 @@ describe('Validating extension', () => {
},
}

const extension = new ExtensionHandler('GH_TOKEN', 'Test config', mock)
const extension = new ExtensionHandler('GH_TOKEN', 'Test config')
await extension.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -299,11 +291,15 @@ describe('Validating extension', () => {
statusCode: 204,
})

const lastOptions = JSON.parse(mock.lastOptions().body)
expect(lastOptions).toStrictEqual({
context: 'Test config - File extension check',
description: 'passed',
state: 'success',
})
expect(fetch).toHaveFetched(
'https://api.github.com/repos/foo/bar/statuses/ee55a1223ce20c3e7cb776349cb7f8efb7b88511',
{
body: {
context: 'Test config - File extension check',
description: 'passed',
state: 'success',
},
}
)
})
})
12 changes: 6 additions & 6 deletions tests/weblate.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetchMock from 'fetch-mock'
import fetchMock from '@fetch-mock/jest'
import { WeblateHandler } from '../functions/classes/WeblateHandler'
import { handler } from '../functions/weblate'

Expand Down Expand Up @@ -123,9 +123,7 @@ describe('Apply label', () => {
})

test('PR is ok', async () => {
const mock = fetchMock
.sandbox()
.mock('https://api.github.com/repos/foo/bar/issues/42/labels', 200)
fetchMock.mockGlobal().route('*', 200)

const callback = jest.fn()
const githubEvent = {
Expand All @@ -147,7 +145,7 @@ describe('Apply label', () => {
},
}

const weblate = new WeblateHandler('GH_TOKEN', mock)
const weblate = new WeblateHandler('GH_TOKEN')
await weblate.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -156,6 +154,8 @@ describe('Apply label', () => {
statusCode: 204,
})

expect(mock.lastOptions().body).toBe('{"labels":["Translations"]}')
expect(fetch).toHaveFetched('https://api.github.com/repos/foo/bar/issues/42/labels', {
body: { labels: ['Translations'] },
})
})
})
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,13 @@
resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.57.1.tgz#de633db3ec2ef6a3c89e2f19038063e8a122e2c2"
integrity sha512-d9zaMRSTIKDLhctzH12MtXvJKSSUhaHcjV+2Z+GK+EEY7XKpP5yR4x+N3TAcHTcu963nIr+TMcCb4DBCYX1z6Q==

"@fetch-mock/jest@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@fetch-mock/jest/-/jest-0.2.0.tgz#70fae50fd7886c313b0186c9d18165a80e080109"
integrity sha512-4oKbWKklbk7inBuK2YRtdjiJFG47N0VzkzTI9TOzY0g3q92605bwtcJXnAZ13gr4C0L0O/Bt24EIt0+aNDSd1Q==
dependencies:
fetch-mock "12.0.0"

"@hapi/accept@^6.0.1":
version "6.0.3"
resolved "https://registry.yarnpkg.com/@hapi/accept/-/accept-6.0.3.tgz#eef0800a4f89cd969da8e5d0311dc877c37279ab"
Expand Down Expand Up @@ -4307,7 +4314,7 @@ fb-watchman@^2.0.0:
dependencies:
bser "2.1.1"

fetch-mock@^12.0.0:
fetch-mock@12.0.0:
version "12.0.0"
resolved "https://registry.yarnpkg.com/fetch-mock/-/fetch-mock-12.0.0.tgz#d531e0c58aa3ecc04fa225fc4b624d070563dbc8"
integrity sha512-JSsjzoRN4rYqHa2/+8ushJGDsK9HGNTdBZo6Hrpu3KFN7Y03nRCt2VJ2WG4OUvyTUukOQ4TQIfjcFcEkMPGZ0Q==
Expand Down

0 comments on commit 61071d1

Please sign in to comment.