test(pagination): test deprecations

This commit is contained in:
Gregor 2019-01-08 00:20:04 -08:00
parent a486f7c03b
commit 3f83dae4e7
1 changed files with 41 additions and 11 deletions

View File

@ -27,16 +27,16 @@ describe('GitHubAPI', () => {
}) })
describe('paginate', () => { describe('paginate', () => {
beforeEach(() => { // Prepare an array of issue objects
// Prepare an array of issue objects const issues = new Array(5).fill(0).map((_, i, arr) => {
const issues = new Array(5).fill(0).map((_, i, arr) => { return {
return { id: i,
id: i, number: i,
number: i, title: `Issue number ${i}`
title: `Issue number ${i}` }
} })
})
beforeEach(() => {
nock('https://api.github.com') nock('https://api.github.com')
.get('/repos/JasonEtco/pizza/issues?per_page=1').reply(200, [issues[0]], { .get('/repos/JasonEtco/pizza/issues?per_page=1').reply(200, [issues[0]], {
link: '<https://api.github.com/repositories/123/issues?per_page=1&page=2>; rel="next"' link: '<https://api.github.com/repositories/123/issues?per_page=1&page=2>; rel="next"'
@ -57,7 +57,7 @@ describe('GitHubAPI', () => {
it('returns an array of pages', async () => { it('returns an array of pages', async () => {
const spy = jest.fn() const spy = jest.fn()
const res = await github.paginate(github.issues.listForRepo({ owner: 'JasonEtco', repo: 'pizza', per_page: 1 }), spy) const res = await github.paginate(github.issues.listForRepo.endpoint.merge({ owner: 'JasonEtco', repo: 'pizza', per_page: 1 }), spy)
expect(Array.isArray(res)).toBeTruthy() expect(Array.isArray(res)).toBeTruthy()
expect(res.length).toBe(5) expect(res.length).toBe(5)
expect(spy).toHaveBeenCalledTimes(5) expect(spy).toHaveBeenCalledTimes(5)
@ -67,9 +67,39 @@ describe('GitHubAPI', () => {
const spy = jest.fn((response, done) => { const spy = jest.fn((response, done) => {
if (response.data[0].id === 2) done() if (response.data[0].id === 2) done()
}) })
const res = await github.paginate(github.issues.listForRepo({ owner: 'JasonEtco', repo: 'pizza', per_page: 1 }), spy) const res = await github.paginate(github.issues.listForRepo.endpoint.merge({ owner: 'JasonEtco', repo: 'pizza', per_page: 1 }), spy)
expect(res.length).toBe(3) expect(res.length).toBe(3)
expect(spy).toHaveBeenCalledTimes(3) expect(spy).toHaveBeenCalledTimes(3)
}) })
it('maps the responses to data by default', async () => {
const res = await github.paginate(github.issues.listForRepo.endpoint.merge({ owner: 'JasonEtco', repo: 'pizza', per_page: 1 }))
expect(res).toEqual(issues)
})
describe('deprecations', () => {
let consoleWarnSpy: any
beforeEach(() => {
consoleWarnSpy = jest.spyOn(global.console, 'warn').mockImplementation(() => null)
})
afterEach(() => {
consoleWarnSpy.mockReset()
})
it('github.paginate(promise)', async () => {
const res = await github.paginate(github.issues.listForRepo({ owner: 'JasonEtco', repo: 'pizza', per_page: 1 }))
expect(Array.isArray(res)).toBeTruthy()
expect(res.length).toBe(5)
expect(consoleWarnSpy).toHaveBeenCalled()
})
it('maps to each response by default when using deprecated syntax', async () => {
const res = await github.paginate(github.issues.listForRepo({ owner: 'JasonEtco', repo: 'pizza', per_page: 1 }))
expect(Array.isArray(res)).toBeTruthy()
expect(res.length).toBe(5)
expect('headers' in res[0]).toBeTruthy()
expect(consoleWarnSpy).toHaveBeenCalled()
})
})
}) })
}) })