Skip to content

Commit

Permalink
Update tests to use the Octokit RequestError class
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMGreene committed Mar 28, 2024
1 parent 9a4705d commit 9c9f8a2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"espree": "^9.6.1"
},
"devDependencies": {
"@octokit/request-error": "^5.0.1",
"@vercel/ncc": "^0.38.1",
"eslint": "^8.57.0",
"eslint-config-prettier": "^8.8.0",
Expand Down
39 changes: 27 additions & 12 deletions src/api-client.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
const core = require('@actions/core')
const apiClient = require('./api-client')
const { RequestError } = require('@octokit/request-error')

const mockGetPages = jest.fn()
const mockCreatePagesSite = jest.fn()

const generateRequestError = statusCode => {
const fakeRequest = { headers: {}, url: '/' }
const fakeResponse = { status: statusCode }
let message = 'Oops'
if (statusCode === 404) {
message = 'Not Found'
}
if (statusCode === 409) {
message = 'Too Busy'
}
const error = new RequestError(message, statusCode, { request: fakeRequest, response: fakeResponse })
return error
}

jest.mock('@actions/github', () => ({
context: {
repo: {
Expand Down Expand Up @@ -48,7 +63,7 @@ describe('apiClient', () => {
})

it('handles a 409 response when the page already exists', async () => {
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 409 } }))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(409)))

// Simply assert that no error is raised
const result = await apiClient.enablePagesSite({
Expand All @@ -59,7 +74,7 @@ describe('apiClient', () => {
})

it('re-raises errors on failure status codes', async () => {
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(404)))

let erred = false
try {
Expand All @@ -86,7 +101,7 @@ describe('apiClient', () => {
})

it('re-raises errors on failure status codes', async () => {
mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404)))

let erred = false
try {
Expand All @@ -105,7 +120,7 @@ describe('apiClient', () => {
it('does not make a request to create a page if it already exists', async () => {
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
mockGetPages.mockImplementationOnce(() => Promise.resolve({ status: 200, data: PAGE_OBJECT }))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(404)))

const result = await apiClient.findOrCreatePagesSite({
githubToken: GITHUB_TOKEN
Expand All @@ -117,7 +132,7 @@ describe('apiClient', () => {

it('makes request to create a page by default if it does not exist', async () => {
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404)))
mockCreatePagesSite.mockImplementationOnce(() => Promise.resolve({ status: 201, data: PAGE_OBJECT }))

const result = await apiClient.findOrCreatePagesSite({
Expand All @@ -130,7 +145,7 @@ describe('apiClient', () => {

it('makes a request to create a page when explicitly enabled if it does not exist', async () => {
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404)))
mockCreatePagesSite.mockImplementationOnce(() => Promise.resolve({ status: 201, data: PAGE_OBJECT }))

const result = await apiClient.findOrCreatePagesSite({
Expand All @@ -143,8 +158,8 @@ describe('apiClient', () => {
})

it('does not make a request to create a page when explicitly disabled even if it does not exist', async () => {
mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 500 } })) // just so they both aren't 404
mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404)))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(500))) // just so they both aren't 404

let erred = false
try {
Expand All @@ -163,8 +178,8 @@ describe('apiClient', () => {
})

it('does not make a second request to get page if create fails for reason other than existence', async () => {
mockGetPages.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 500 } })) // just so they both aren't 404
mockGetPages.mockImplementationOnce(() => Promise.reject(generateRequestError(404)))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(500))) // just so they both aren't 404

let erred = false
try {
Expand All @@ -184,9 +199,9 @@ describe('apiClient', () => {
it('makes second request to get page if create fails because of existence', async () => {
const PAGE_OBJECT = { html_url: 'https://actions.github.io/is-awesome/' }
mockGetPages
.mockImplementationOnce(() => Promise.reject({ response: { status: 404 } }))
.mockImplementationOnce(() => Promise.reject(generateRequestError(404)))
.mockImplementationOnce(() => Promise.resolve({ status: 200, data: PAGE_OBJECT }))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject({ response: { status: 409 } }))
mockCreatePagesSite.mockImplementationOnce(() => Promise.reject(generateRequestError(409)))

const result = await apiClient.findOrCreatePagesSite({
githubToken: GITHUB_TOKEN
Expand Down

0 comments on commit 9c9f8a2

Please sign in to comment.