Skip to content
Snippets Groups Projects
Select Git revision
  • b5870d90d98f2cbb44f9f809f16fbe2a48236ea5
  • master default
2 results

github.spec.js

Blame
  • github.spec.js 45.10 KiB
    const logger = require('../_fixtures/logger');
    
    describe('api/github', () => {
      let github;
      let get;
      beforeEach(() => {
        // clean up env
        delete process.env.GITHUB_TOKEN;
        delete process.env.GITHUB_ENDPOINT;
    
        // reset module
        jest.resetModules();
        jest.mock('../../lib/api/gh-got-wrapper');
        get = require('../../lib/api/gh-got-wrapper');
        github = require('../../lib/api/github');
      });
    
      describe('getInstallations', () => {
        it('should return an array of installations', async () => {
          get.mockImplementationOnce(() => ({
            body: ['a', 'b'],
          }));
          const installations = await github.getInstallations('sometoken');
          expect(get.mock.calls).toMatchSnapshot();
          expect(installations).toMatchSnapshot();
        });
        it('should return a 404', async () => {
          get.mockImplementationOnce(() =>
            Promise.reject({
              statusCode: 404,
            })
          );
          let err;
          try {
            await github.getInstallations('sometoken');
          } catch (e) {
            err = e;
          }
          expect(err.statusCode).toBe(404);
        });
      });
    
      describe('getInstallationToken', () => {
        it('should return an installation token', async () => {
          get.post.mockImplementationOnce(() => ({
            body: {
              token: 'aUserToken',
            },
          }));
          const installationToken = await github.getInstallationToken(
            'sometoken',
            123456
          );
          expect(get.mock.calls).toMatchSnapshot();
          expect(installationToken).toMatchSnapshot();
        });
        it('should return an error if given one', async () => {
          get.post.mockImplementationOnce(() => {
            throw new Error('error');
          });
          let err;
          try {
            await github.getInstallationToken('sometoken', 123456);
          } catch (e) {
            err = e;
          }
          expect(err.message).toBe('error');
        });
      });