1import GithubClient from './githubClient'; 2 3const fakeClient = jest.fn(); 4 5beforeEach(() => { 6 delete process.env.GITHUB_USERNAME; 7 delete process.env.GITHUB_ACCESS_TOKEN; 8}); 9 10afterEach(() => { 11 delete process.env.GITHUB_USERNAME; 12 delete process.env.GITHUB_ACCESS_TOKEN; 13}); 14 15describe('GithubClient', () => { 16 it('should initialise a GithubClient', () => { 17 const github = new GithubClient(); 18 const githubEnterprise = new GithubClient({ enterprise: true }); 19 expect(github).toBeInstanceOf(GithubClient); 20 expect(githubEnterprise).toBeInstanceOf(GithubClient); 21 }); 22 23 describe('#client', () => { 24 it('it should contain a grafana client', () => { 25 // @ts-ignore 26 const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient); 27 28 const github = new GithubClient(); 29 const client = github.client; 30 31 expect(spy).toHaveBeenCalledWith({ 32 baseURL: 'https://api.github.com/repos/grafana/grafana', 33 timeout: 10000, 34 }); 35 expect(client).toEqual(fakeClient); 36 }); 37 38 it('it should contain a grafana enterprise client', () => { 39 // @ts-ignore 40 const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient); 41 42 const github = new GithubClient({ enterprise: true }); 43 const client = github.client; 44 45 expect(spy).toHaveBeenCalledWith({ 46 baseURL: 'https://api.github.com/repos/grafana/grafana-enterprise', 47 timeout: 10000, 48 }); 49 expect(client).toEqual(fakeClient); 50 }); 51 52 describe('when the credentials are required', () => { 53 it('should create the client when the credentials are defined', () => { 54 const username = 'grafana'; 55 const token = 'averysecureaccesstoken'; 56 57 process.env.GITHUB_USERNAME = username; 58 process.env.GITHUB_ACCESS_TOKEN = token; 59 60 // @ts-ignore 61 const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient); 62 63 const github = new GithubClient({ required: true }); 64 const client = github.client; 65 66 expect(spy).toHaveBeenCalledWith({ 67 baseURL: 'https://api.github.com/repos/grafana/grafana', 68 timeout: 10000, 69 auth: { username, password: token }, 70 }); 71 72 expect(client).toEqual(fakeClient); 73 }); 74 75 it('should create the enterprise client when the credentials are defined', () => { 76 const username = 'grafana'; 77 const token = 'averysecureaccesstoken'; 78 79 process.env.GITHUB_USERNAME = username; 80 process.env.GITHUB_ACCESS_TOKEN = token; 81 82 // @ts-ignore 83 const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient); 84 85 const github = new GithubClient({ required: true, enterprise: true }); 86 const client = github.client; 87 88 expect(spy).toHaveBeenCalledWith({ 89 baseURL: 'https://api.github.com/repos/grafana/grafana-enterprise', 90 timeout: 10000, 91 auth: { username, password: token }, 92 }); 93 94 expect(client).toEqual(fakeClient); 95 }); 96 97 describe('when the credentials are not defined', () => { 98 it('should throw an error', () => { 99 expect(() => { 100 // eslint-disable-next-line 101 new GithubClient({ required: true }); 102 }).toThrow(/operation needs a GITHUB_USERNAME and GITHUB_ACCESS_TOKEN environment variables/); 103 }); 104 }); 105 }); 106 }); 107}); 108