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

gitlab.spec.js

Blame
  • gitlab.spec.js 25.41 KiB
    const logger = require('../_fixtures/logger');
    
    describe('api/gitlab', () => {
      let gitlab;
      let get;
      beforeEach(() => {
        // clean up env
        delete process.env.GITLAB_TOKEN;
        delete process.env.GITLAB_ENDPOINT;
    
        // reset module
        jest.resetModules();
        jest.mock('../../lib/api/gl-got-wrapper');
        gitlab = require('../../lib/api/gitlab');
        get = require('../../lib/api/gl-got-wrapper');
      });
    
      describe('getRepos', () => {
        async function getRepos(...args) {
          // repo info
          get.mockImplementationOnce(() => ({
            body: [
              {
                path_with_namespace: 'a/b',
              },
              {
                path_with_namespace: 'c/d',
              },
            ],
          }));
          return gitlab.getRepos(...args);
        }
        it('should throw an error if no token is provided', async () => {
          let err;
          try {
            await gitlab.getRepos();
          } catch (e) {
            err = e;
          }
          expect(err.message).toBe('No token found for getRepos');
        });
        it('should throw an error if it receives an error', async () => {
          get.mockImplementation(() => {
            throw new Error('getRepos error');
          });
          let err;
          try {
            await gitlab.getRepos('sometoken');
          } catch (e) {
            err = e;
          }
          expect(err.message).toBe('getRepos error');
        });
        it('should return an array of repos', async () => {
          const repos = await getRepos('sometoken');
          expect(get.mock.calls).toMatchSnapshot();
          expect(repos).toMatchSnapshot();
        });
        it('should support a custom endpoint', async () => {
          const repos = await getRepos('sometoken', 'someendpoint');
          expect(get.mock.calls).toMatchSnapshot();
          expect(repos).toMatchSnapshot();
        });
        it('should fetch multiple pages', async () => {
          const repoCount = 250;
          const projects = [];
          for (let i = 0; i < repoCount; i += 1) {
            projects.push({ path_with_namespace: `project${i}` });
          }
          get.mockImplementationOnce(() => ({