mirror of
https://github.com/zhigang1992/probot.git
synced 2026-06-16 02:44:19 +08:00
Convert the rest of the files in `/src` and disable allowJs Move everything to named exports Update bin to use named exports Make all tests js and use named exports
67 lines
2.3 KiB
JavaScript
67 lines
2.3 KiB
JavaScript
const {EnhancedGitHubClient} = require('../src/github')
|
|
const nock = require('nock')
|
|
const Bottleneck = require('bottleneck')
|
|
|
|
describe('EnhancedGitHubClient', () => {
|
|
let github
|
|
|
|
beforeEach(() => {
|
|
const logger = {
|
|
debug: jest.fn(),
|
|
trace: jest.fn()
|
|
}
|
|
|
|
// Set a shorter limiter, otherwise tests are _slow_
|
|
const limiter = new Bottleneck({ maxConcurrent: 1, minTime: 1 })
|
|
|
|
github = new EnhancedGitHubClient({ logger, limiter })
|
|
})
|
|
|
|
describe('paginate', () => {
|
|
beforeEach(() => {
|
|
// Prepare an array of issue objects
|
|
const issues = new Array(5).fill().map((_, i, arr) => {
|
|
return {
|
|
title: `Issue number ${i}`,
|
|
id: i,
|
|
number: i
|
|
}
|
|
})
|
|
|
|
nock('https://api.github.com')
|
|
.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"'
|
|
})
|
|
.get('/repositories/123/issues?per_page=1&page=2').reply(200, issues[1], {
|
|
link: '<https://api.github.com/repositories/123/issues?per_page=1&page=3>; rel="next"'
|
|
})
|
|
.get('/repositories/123/issues?per_page=1&page=3').reply(200, issues[2], {
|
|
link: '<https://api.github.com/repositories/123/issues?per_page=1&page=4>; rel="next"'
|
|
})
|
|
.get('/repositories/123/issues?per_page=1&page=4').reply(200, issues[3], {
|
|
link: '<https://api.github.com/repositories/123/issues?per_page=1&page=5>; rel="next"'
|
|
})
|
|
.get('/repositories/123/issues?per_page=1&page=5').reply(200, issues[4], {
|
|
link: ''
|
|
})
|
|
})
|
|
|
|
it('returns an array of pages', async () => {
|
|
const spy = jest.fn()
|
|
const res = await github.paginate(github.issues.getForRepo({ owner: 'JasonEtco', repo: 'pizza', per_page: 1 }), spy)
|
|
expect(Array.isArray(res)).toBeTruthy()
|
|
expect(res.length).toBe(5)
|
|
expect(spy).toHaveBeenCalledTimes(5)
|
|
})
|
|
|
|
it('stops iterating if the done() function is called in the callback', async () => {
|
|
const spy = jest.fn((res, done) => {
|
|
if (res.data.id === 2) done()
|
|
})
|
|
const res = await github.paginate(github.issues.getForRepo({ owner: 'JasonEtco', repo: 'pizza', per_page: 1 }), spy)
|
|
expect(res.length).toBe(3)
|
|
expect(spy).toHaveBeenCalledTimes(3)
|
|
})
|
|
})
|
|
})
|