/ __tests__ / set-tokens.test.ts
set-tokens.test.ts
  1  import {getPublishRepo, setPersonalToken, setGithubToken} from '../src/set-tokens';
  2  
  3  beforeEach(() => {
  4    jest.resetModules();
  5  });
  6  
  7  // afterEach(() => {
  8  
  9  // });
 10  
 11  describe('getPublishRepo()', () => {
 12    test('return repository name', () => {
 13      const test = getPublishRepo('', 'owner', 'repo');
 14      expect(test).toMatch('owner/repo');
 15    });
 16  
 17    test('return external repository name', () => {
 18      const test = getPublishRepo('extOwner/extRepo', 'owner', 'repo');
 19      expect(test).toMatch('extOwner/extRepo');
 20    });
 21  });
 22  
 23  describe('setGithubToken()', () => {
 24    test('return remote url with GITHUB_TOKEN gh-pages', () => {
 25      const expected = 'https://x-access-token:GITHUB_TOKEN@github.com/owner/repo.git';
 26      const test = setGithubToken(
 27        'GITHUB_TOKEN',
 28        'owner/repo',
 29        'gh-pages',
 30        '',
 31        'refs/heads/master',
 32        'push'
 33      );
 34      expect(test).toMatch(expected);
 35    });
 36  
 37    test('return remote url with GITHUB_TOKEN master', () => {
 38      const expected = 'https://x-access-token:GITHUB_TOKEN@github.com/owner/repo.git';
 39      const test = setGithubToken(
 40        'GITHUB_TOKEN',
 41        'owner/repo',
 42        'master',
 43        '',
 44        'refs/heads/source',
 45        'push'
 46      );
 47      expect(test).toMatch(expected);
 48    });
 49  
 50    test('return remote url with GITHUB_TOKEN gh-pages (RegExp)', () => {
 51      const expected = 'https://x-access-token:GITHUB_TOKEN@github.com/owner/repo.git';
 52      const test = setGithubToken(
 53        'GITHUB_TOKEN',
 54        'owner/repo',
 55        'gh-pages',
 56        '',
 57        'refs/heads/gh-pages-base',
 58        'push'
 59      );
 60      expect(test).toMatch(expected);
 61    });
 62  
 63    test('throw error gh-pages-base to gh-pages-base (RegExp)', () => {
 64      expect(() => {
 65        setGithubToken(
 66          'GITHUB_TOKEN',
 67          'owner/repo',
 68          'gh-pages-base',
 69          '',
 70          'refs/heads/gh-pages-base',
 71          'push'
 72        );
 73      }).toThrowError('You deploy from gh-pages-base to gh-pages-base');
 74    });
 75  
 76    test('throw error master to master', () => {
 77      expect(() => {
 78        setGithubToken('GITHUB_TOKEN', 'owner/repo', 'master', '', 'refs/heads/master', 'push');
 79      }).toThrowError('You deploy from master to master');
 80    });
 81  
 82    test('throw error external repository with GITHUB_TOKEN', () => {
 83      expect(() => {
 84        setGithubToken(
 85          'GITHUB_TOKEN',
 86          'owner/repo',
 87          'gh-pages',
 88          'extOwner/extRepo',
 89          'refs/heads/master',
 90          'push'
 91        );
 92      }).toThrowError(`\
 93  The generated GITHUB_TOKEN (github_token) does not support to push to an external repository.
 94  Use deploy_key or personal_token.
 95  `);
 96    });
 97  
 98    test('return remote url with GITHUB_TOKEN pull_request', () => {
 99      const expected = 'https://x-access-token:GITHUB_TOKEN@github.com/owner/repo.git';
100      const test = setGithubToken(
101        'GITHUB_TOKEN',
102        'owner/repo',
103        'gh-pages',
104        '',
105        'refs/pull/29/merge',
106        'pull_request'
107      );
108      expect(test).toMatch(expected);
109    });
110  });
111  
112  describe('setPersonalToken()', () => {
113    test('return remote url with personal access token', () => {
114      const expected = 'https://x-access-token:pat@github.com/owner/repo.git';
115      const test = setPersonalToken('pat', 'owner/repo');
116      expect(test).toMatch(expected);
117    });
118  });