Skip to content

Commit

Permalink
update api tests to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-codecov committed May 10, 2024
1 parent 550d378 commit cbf3d2b
Showing 1 changed file with 99 additions and 74 deletions.
173 changes: 99 additions & 74 deletions src/shared/api/api.test.js → src/shared/api/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { waitFor } from '@testing-library/react'
import { graphql, rest } from 'msw'
import { setupServer } from 'msw/node'

Expand Down Expand Up @@ -41,7 +42,7 @@ const server = setupServer(
return res(ctx.status(200), ctx.json(req.body))
}),
rest.delete('/internal/test', (req, res, ctx) => {
return res(ctx.status(204), null)
return res(ctx.status(204), ctx.json(null))
}),
graphql.query('MyInfo', (req, res, ctx) => {
return res(
Expand Down Expand Up @@ -109,12 +110,20 @@ const server = setupServer(
})
)

beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())
beforeAll(() => {
server.listen()
})

afterEach(() => {
server.resetHandlers()
})

afterAll(() => {
server.close()
})

let result, error
function callApi(provider = null) {
let result: any, error: any
function callApi(provider: string | null = null) {
result = null
error = null
return Api.get({
Expand Down Expand Up @@ -226,21 +235,23 @@ describe('when using a graphql request', () => {
})

describe('when different strings entered as provider', () => {
let fetchMock
afterEach(() => {
global.fetch.mockRestore()
let fetchSpy: jest.SpyInstance

afterAll(() => {
fetchSpy.mockRestore()
})

it('does not have the provider in the url for non-provider', async () => {
fetchMock = jest.fn((url, options) => {
const fetchMock = jest.fn((url, options) => {
expect(url).toBe(`${config.API_URL}/graphql/`)
return Promise.resolve({
ok: true,
json: async () => ({ data: { example: 'dummy data' } }),
})
})

jest.spyOn(global, 'fetch').mockImplementation(fetchMock)
// @ts-expect-error - jest spy
fetchSpy = jest.spyOn(global, 'fetch').mockImplementation(fetchMock)

await Api.graphql({
provider: 'random hacks and stuff',
Expand All @@ -249,18 +260,20 @@ describe('when using a graphql request', () => {

expect(fetchMock).toHaveBeenCalled()
})

test.each(AllProvidersArray)(
'has the provider in the url for %s',
async (provider) => {
fetchMock = jest.fn((url, options) => {
const fetchMock = jest.fn((url, options) => {
expect(url).toBe(`${config.API_URL}/graphql/${provider}`)
return Promise.resolve({
ok: true,
json: async () => ({ data: { example: 'dummy data' } }),
})
})

jest.spyOn(global, 'fetch').mockImplementation(fetchMock)
// @ts-expect-error - jest spy
fetchSpy = jest.spyOn(global, 'fetch').mockImplementation(fetchMock)

await Api.graphql({
provider: provider,
Expand All @@ -274,20 +287,25 @@ describe('when using a graphql request', () => {

describe('the request is unsuccessful', () => {
it('returns the error status code', async () => {
const data = Api.graphql({
provider: 'gh',
query: 'query ErrorQuery { me }',
})
let error: any
try {
await Api.graphql({
provider: 'gh',
query: 'query ErrorQuery { me }',
})
} catch (err) {
error = err
}

await expect(data).rejects.toStrictEqual({
expect(error).toStrictEqual({
data: { data: { me: 'Codecov' } },
status: 400,
})
})
})

describe('when sending an encoded string', () => {
beforeEach(() => {
it('returns a decoded string', async () => {
const query = `
query CoverageForFile($owner: String!, $repo: String!, $ref: String!) {
owner(username: $owner) {
Expand All @@ -300,9 +318,9 @@ describe('when using a graphql request', () => {
}
}
}
}
`
result = Api.graphql({
}`

const result = await Api.graphql({
provider: 'gh',
query,
variables: {
Expand All @@ -311,98 +329,102 @@ describe('when using a graphql request', () => {
ref: 'encoded%2Fstring',
},
})
return result
})

it('returns a decoded string', () => {
return expect(result).resolves.toEqual({
data: {
owner: {
repository: {
commit: {
commitid: 'encoded/string',
await waitFor(() =>
expect(result).toStrictEqual({
data: {
owner: {
repository: {
__typename: 'Repository',
commit: { commitid: 'encoded/string' },
},
},
},
},
})
})
)
})
})
})

describe('when using a graphql mutation', () => {
describe('when the mutation has unauthenticated error', () => {
const mutation = `
mutation CreateTokenUnauthorized {
createApiToken {
error {
__typename
it('throws an exception', async () => {
const mutation = `
mutation CreateTokenUnauthorized {
createApiToken {
error {
__typename
}
token
}
token
}
`

let error: any
try {
await Api.graphqlMutation({
provider: 'gh',
query: mutation,
mutationPath: 'createApiToken',
})
} catch (err) {
error = err
}
`
beforeEach(() => {
result = Api.graphqlMutation({
provider: 'gh',
query: mutation,
mutationPath: 'createApiToken',
})
})

it('throws an expection retuns', () => {
return expect(result).rejects.toEqual({
__typename: 'UnauthorizedError',
})
await waitFor(() =>
expect(error).toEqual({
__typename: 'UnauthorizedError',
})
)
})
})

describe('when the mutation has no error', () => {
const mutation = `
it('resolves with the data', async () => {
const mutation = `
mutation CreateToken {
createApiToken {
error {
__typename
}
token
}
}
`
beforeEach(() => {
result = Api.graphqlMutation({
}`

const result = await Api.graphqlMutation({
provider: 'gh',
query: mutation,
mutationPath: 'createApiToken',
})
})

it('resolves with the data', () => {
return expect(result).resolves.toEqual({
data: {
createApiToken: {
token: 123,
error: null,
await waitFor(() =>
expect(result).toEqual({
data: {
createApiToken: {
token: 123,
error: null,
},
},
},
})
})
)
})
})

describe('when the mutation supports serviceless', () => {
beforeAll(() => {
result = Api.graphqlMutation({
it('resolves with the data', async () => {
const result = await Api.graphqlMutation({
query: 'query MyInfo { me }',
mutationPath: 'me',
supportsServiceless: true,
})
})

it('resolves with the data', async () => {
return expect(result).resolves.toEqual({
data: {
me: 'Codecov',
},
})
await waitFor(() =>
expect(result).toEqual({
data: {
me: 'Codecov',
},
})
)
})
})

Expand All @@ -411,6 +433,7 @@ describe('when using a graphql mutation', () => {

beforeAll(() => {
const location = window.location
// @ts-expect-error - test magic
delete global.window.location
global.window.location = Object.assign({}, location)
})
Expand All @@ -426,6 +449,8 @@ describe('when using a graphql mutation', () => {
} catch (err) {
error = err
}

// @ts-expect-error - test magic
expect(error.status).toBe(403)
expect(window.location.href).toBe('/login')
})
Expand Down

0 comments on commit cbf3d2b

Please sign in to comment.