Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Remove repositoryDeprecated from api tests #2872

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
183 changes: 106 additions & 77 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 All @@ -64,6 +65,7 @@ const server = setupServer(
ctx.data({
owner: {
repository: {
__typename: 'Repository',
commit: {
commitid: ref,
},
Expand Down Expand Up @@ -108,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 @@ -225,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 @@ -248,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 @@ -273,32 +287,40 @@ 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) {
repository: repositoryDeprecated(name: $repo){
commit(id: $ref) {
commitid
repository(name: $repo) {
__typename
... on Repository {
commit(id: $ref) {
commitid
}
}
}
}
}
`
result = Api.graphql({
}`

const result = await Api.graphql({
provider: 'gh',
query,
variables: {
Expand All @@ -307,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 @@ -407,6 +433,7 @@ describe('when using a graphql mutation', () => {

beforeAll(() => {
const location = window.location
// @ts-expect-error - test magic
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧙

delete global.window.location
global.window.location = Object.assign({}, location)
})
Expand All @@ -422,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