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 9255594 commit 6e29624
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 219 deletions.
4 changes: 2 additions & 2 deletions functions/classes/FixupHandler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Handler } from './Handler'

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

this.namespace = namespace
}
Expand Down
25 changes: 5 additions & 20 deletions functions/classes/Handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,16 @@ import { Octokit } from '@octokit/rest'
import { graphql } from '@octokit/graphql'

export class Handler {
constructor(githubToken, fetch = null) {
const options = {
constructor(githubToken) {
this.githubClient = new Octokit({
auth: githubToken,
}

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

this.githubClient = new Octokit(options)
})

const graphqlOptions = {
this.graphql = graphql.defaults({
headers: {
authorization: `token ${githubToken}`,
},
}

if (fetch) {
graphqlOptions.request = {
fetch,
}
}
this.graphql = graphql.defaults(graphqlOptions)
})
}

// eslint-disable-next-line class-methods-use-this
Expand Down
4 changes: 2 additions & 2 deletions functions/classes/LabelHandler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Handler } from './Handler'

export class LabelHandler extends Handler {
constructor(githubToken, namespace = '', blockLabels = '', fetch = null) {
super(githubToken, fetch)
constructor(githubToken, namespace = '', blockLabels = '') {
super(githubToken)

this.namespace = namespace
this.blockLabels = blockLabels
Expand Down
4 changes: 2 additions & 2 deletions functions/classes/SpecificationHandler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Handler } from './Handler'

export class SpecificationHandler extends Handler {
constructor(githubToken, namespace = '', titleLength = 8, bodyLength = 8, fetch = null) {
super(githubToken, fetch)
constructor(githubToken, namespace = '', titleLength = 8, bodyLength = 8) {
super(githubToken)

this.namespace = namespace
this.titleLength = titleLength
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 @@ -23,7 +24,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",
"node-fetch": "2",
"prettier": "^3.3.3",
Expand Down
55 changes: 30 additions & 25 deletions tests/auto-merge.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 { AutomergeHandler } from '../functions/classes/AutomergeHandler'
import { handler } from '../functions/auto-merge'

Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Validating GitHub event', () => {

describe('Auto merge', () => {
test('got a PR approved & merged', async () => {
const mock = fetchMock.sandbox().mock('https://api.github.com/graphql', 200)
fetchMock.mockGlobal().route('*', 200)

const callback = jest.fn()
const githubEvent = {
Expand All @@ -139,7 +139,7 @@ describe('Auto merge', () => {
},
}

const autoMerge = new AutomergeHandler('GH_TOKEN', mock)
const autoMerge = new AutomergeHandler('GH_TOKEN')
await autoMerge.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -148,19 +148,20 @@ describe('Auto merge', () => {
statusCode: 204,
})

const calls = mock.calls()
const firstBody = JSON.parse(calls[0][1].body)
const calls = fetch.fetchMock.callHistory.calls()

const firstBody = JSON.parse(calls[0].options.body)
expect(firstBody.query).toEqual(expect.stringContaining('addPullRequestReview'))
expect(firstBody.query).toEqual(expect.stringContaining('event: APPROVE'))
expect(firstBody.variables.pullRequestId).toBe('PR_kwDOHDoTgM5BdXq1')

const secondBody = JSON.parse(calls[1][1].body)
const secondBody = JSON.parse(calls[1].options.body)
expect(secondBody.query).toEqual(expect.stringContaining('enablePullRequestAutoMerge'))
expect(secondBody.variables.pullRequestId).toBe('PR_kwDOHDoTgM5BdXq1')
})

test('got a PR approved & merged with one deps in grouped deps', async () => {
const mock = fetchMock.sandbox().mock('https://api.github.com/graphql', 200)
fetchMock.mockGlobal().route('*', 200)

const callback = jest.fn()
const githubEvent = {
Expand All @@ -187,7 +188,7 @@ describe('Auto merge', () => {
},
}

const autoMerge = new AutomergeHandler('GH_TOKEN', mock)
const autoMerge = new AutomergeHandler('GH_TOKEN')
await autoMerge.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -196,19 +197,20 @@ describe('Auto merge', () => {
statusCode: 204,
})

const calls = mock.calls()
const firstBody = JSON.parse(calls[0][1].body)
const calls = fetch.fetchMock.callHistory.calls()

const firstBody = JSON.parse(calls[0].options.body)
expect(firstBody.query).toEqual(expect.stringContaining('addPullRequestReview'))
expect(firstBody.query).toEqual(expect.stringContaining('event: APPROVE'))
expect(firstBody.variables.pullRequestId).toBe('PR_kwDOHDoTgM5BdXq1')

const secondBody = JSON.parse(calls[1][1].body)
const secondBody = JSON.parse(calls[1].options.body)
expect(secondBody.query).toEqual(expect.stringContaining('enablePullRequestAutoMerge'))
expect(secondBody.variables.pullRequestId).toBe('PR_kwDOHDoTgM5BdXq1')
})

test('got a PR approved & merged from grouped deps', async () => {
const mock = fetchMock.sandbox().mock('https://api.github.com/graphql', 200)
fetchMock.mockGlobal().route('*', 200)

const callback = jest.fn()
const githubEvent = {
Expand Down Expand Up @@ -335,7 +337,7 @@ You can trigger Dependabot actions by commenting on this PR:
},
}

const autoMerge = new AutomergeHandler('GH_TOKEN', mock)
const autoMerge = new AutomergeHandler('GH_TOKEN')
await autoMerge.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -344,13 +346,14 @@ You can trigger Dependabot actions by commenting on this PR:
statusCode: 204,
})

const calls = mock.calls()
const firstBody = JSON.parse(calls[0][1].body)
const calls = fetch.fetchMock.callHistory.calls()

const firstBody = JSON.parse(calls[0].options.body)
expect(firstBody.query).toEqual(expect.stringContaining('addPullRequestReview'))
expect(firstBody.query).toEqual(expect.stringContaining('event: APPROVE'))
expect(firstBody.variables.pullRequestId).toBe('PR_kwDOHDoTgM5BdXq1')

const secondBody = JSON.parse(calls[1][1].body)
const secondBody = JSON.parse(calls[1].options.body)
expect(secondBody.query).toEqual(expect.stringContaining('enablePullRequestAutoMerge'))
expect(secondBody.variables.pullRequestId).toBe('PR_kwDOHDoTgM5BdXq1')
})
Expand Down Expand Up @@ -604,7 +607,7 @@ Updates \`@storybook/addon-essentials\` from 6.5.12 to 7.5.13`,
})

test('PR has already auto_merge enabled', async () => {
const mock = fetchMock.sandbox().mock('https://api.github.com/graphql', 200)
fetchMock.mockGlobal().route('*', 200)

const callback = jest.fn()
const githubEvent = {
Expand All @@ -631,7 +634,7 @@ Updates \`@storybook/addon-essentials\` from 6.5.12 to 7.5.13`,
},
}

const autoMerge = new AutomergeHandler('GH_TOKEN', mock)
const autoMerge = new AutomergeHandler('GH_TOKEN')
await autoMerge.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -640,15 +643,16 @@ Updates \`@storybook/addon-essentials\` from 6.5.12 to 7.5.13`,
statusCode: 204,
})

const calls = mock.calls()
const firstBody = JSON.parse(calls[0][1].body)
const calls = fetch.fetchMock.callHistory.calls()

const firstBody = JSON.parse(calls[0].options.body)
expect(firstBody.query).toEqual(expect.stringContaining('addPullRequestReview'))
expect(firstBody.query).toEqual(expect.stringContaining('event: APPROVE'))
expect(firstBody.variables.pullRequestId).toBe('PR_kwDOHDoTgM5BdXq1')
})

test('PR is about a minor change from alpha versions', async () => {
const mock = fetchMock.sandbox().mock('https://api.github.com/graphql', 200)
fetchMock.mockGlobal().route('*', 200)

const callback = jest.fn()
const githubEvent = {
Expand Down Expand Up @@ -677,7 +681,7 @@ Updates \`@storybook/addon-essentials\` from 6.5.12 to 7.5.13`,
},
}

const autoMerge = new AutomergeHandler('GH_TOKEN', mock)
const autoMerge = new AutomergeHandler('GH_TOKEN')
await autoMerge.handle(githubEvent, callback)

expect(callback).toHaveBeenCalledTimes(1)
Expand All @@ -686,13 +690,14 @@ Updates \`@storybook/addon-essentials\` from 6.5.12 to 7.5.13`,
statusCode: 204,
})

const calls = mock.calls()
const firstBody = JSON.parse(calls[0][1].body)
const calls = fetch.fetchMock.callHistory.calls()

const firstBody = JSON.parse(calls[0].options.body)
expect(firstBody.query).toEqual(expect.stringContaining('addPullRequestReview'))
expect(firstBody.query).toEqual(expect.stringContaining('event: APPROVE'))
expect(firstBody.variables.pullRequestId).toBe('PR_kwDOHDoTgM5BdXq1')

const secondBody = JSON.parse(calls[1][1].body)
const secondBody = JSON.parse(calls[1].options.body)
expect(secondBody.query).toEqual(expect.stringContaining('enablePullRequestAutoMerge'))
expect(secondBody.variables.pullRequestId).toBe('PR_kwDOHDoTgM5BdXq1')
})
Expand Down
Loading

0 comments on commit 6e29624

Please sign in to comment.