githubClient.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import GithubClient from './githubClient';
  2. const fakeClient = jest.fn();
  3. beforeEach(() => {
  4. delete process.env.GITHUB_USERNAME;
  5. delete process.env.GITHUB_ACCESS_TOKEN;
  6. });
  7. afterEach(() => {
  8. delete process.env.GITHUB_USERNAME;
  9. delete process.env.GITHUB_ACCESS_TOKEN;
  10. });
  11. describe('GithubClient', () => {
  12. it('should initialise a GithubClient', () => {
  13. const github = new GithubClient();
  14. expect(github).toBeInstanceOf(GithubClient);
  15. });
  16. describe('#client', () => {
  17. it('it should contain a client', () => {
  18. const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient);
  19. const github = new GithubClient();
  20. const client = github.client;
  21. expect(spy).toHaveBeenCalledWith({
  22. baseURL: 'https://api.github.com/repos/grafana/grafana',
  23. timeout: 10000,
  24. });
  25. expect(client).toEqual(fakeClient);
  26. });
  27. describe('when the credentials are required', () => {
  28. it('should create the client when the credentials are defined', () => {
  29. const username = 'grafana';
  30. const token = 'averysecureaccesstoken';
  31. process.env.GITHUB_USERNAME = username;
  32. process.env.GITHUB_ACCESS_TOKEN = token;
  33. const spy = jest.spyOn(GithubClient.prototype, 'createClient').mockImplementation(() => fakeClient);
  34. const github = new GithubClient(true);
  35. const client = github.client;
  36. expect(spy).toHaveBeenCalledWith({
  37. baseURL: 'https://api.github.com/repos/grafana/grafana',
  38. timeout: 10000,
  39. auth: { username, password: token },
  40. });
  41. expect(client).toEqual(fakeClient);
  42. });
  43. describe('when the credentials are not defined', () => {
  44. it('should throw an error', () => {
  45. expect(() => {
  46. new GithubClient(true);
  47. }).toThrow(/operation needs a GITHUB_USERNAME and GITHUB_ACCESS_TOKEN environment variables/);
  48. });
  49. });
  50. });
  51. });
  52. });