|
| 1 | +jest.mock('../../src/utils/logger'); |
| 2 | + |
| 3 | +import nock from 'nock'; |
| 4 | + |
| 5 | +afterEach(() => { |
| 6 | + jest.resetModules(); |
| 7 | + delete process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH; |
| 8 | + delete process.env.TWILIO_SERVERLESS_TEMPLATE_REPO; |
| 9 | +}); |
| 10 | + |
| 11 | +describe('base API configuration', () => { |
| 12 | + test('has the right default values', () => { |
| 13 | + const { |
| 14 | + TEMPLATES_URL, |
| 15 | + CONTENT_BASE_URL, |
| 16 | + } = require('../../src/templating/data'); |
| 17 | + |
| 18 | + expect(TEMPLATES_URL).toBe( |
| 19 | + 'https://raw.githubusercontent.com/twilio-labs/function-templates/master/templates.json' |
| 20 | + ); |
| 21 | + expect(CONTENT_BASE_URL).toBe( |
| 22 | + 'https://api.github.com/repos/twilio-labs/function-templates/contents' |
| 23 | + ); |
| 24 | + }); |
| 25 | + |
| 26 | + test('allows to override repo', () => { |
| 27 | + process.env.TWILIO_SERVERLESS_TEMPLATE_REPO = 'dkundel/function-templates'; |
| 28 | + |
| 29 | + const { |
| 30 | + TEMPLATES_URL, |
| 31 | + CONTENT_BASE_URL, |
| 32 | + } = require('../../src/templating/data'); |
| 33 | + |
| 34 | + expect(TEMPLATES_URL).toBe( |
| 35 | + 'https://raw.githubusercontent.com/dkundel/function-templates/master/templates.json' |
| 36 | + ); |
| 37 | + expect(CONTENT_BASE_URL).toBe( |
| 38 | + 'https://api.github.com/repos/dkundel/function-templates/contents' |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + test('allows to override base branch', () => { |
| 43 | + process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH = 'next'; |
| 44 | + |
| 45 | + const { |
| 46 | + TEMPLATES_URL, |
| 47 | + CONTENT_BASE_URL, |
| 48 | + } = require('../../src/templating/data'); |
| 49 | + |
| 50 | + expect(TEMPLATES_URL).toBe( |
| 51 | + 'https://raw.githubusercontent.com/twilio-labs/function-templates/next/templates.json' |
| 52 | + ); |
| 53 | + expect(CONTENT_BASE_URL).toBe( |
| 54 | + 'https://api.github.com/repos/twilio-labs/function-templates/contents' |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + test('allows to override branch and repo', () => { |
| 59 | + process.env.TWILIO_SERVERLESS_TEMPLATE_REPO = 'philnash/function-templates'; |
| 60 | + process.env.TWILIO_SERVERLESS_TEMPLATE_BRANCH = 'development'; |
| 61 | + |
| 62 | + const { |
| 63 | + TEMPLATES_URL, |
| 64 | + CONTENT_BASE_URL, |
| 65 | + } = require('../../src/templating/data'); |
| 66 | + |
| 67 | + expect(TEMPLATES_URL).toBe( |
| 68 | + 'https://raw.githubusercontent.com/philnash/function-templates/development/templates.json' |
| 69 | + ); |
| 70 | + expect(CONTENT_BASE_URL).toBe( |
| 71 | + 'https://api.github.com/repos/philnash/function-templates/contents' |
| 72 | + ); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('getTemplateFiles', () => { |
| 77 | + const gitHubApi = nock('https://api.github.com'); |
| 78 | + |
| 79 | + afterEach(() => { |
| 80 | + gitHubApi.done(); |
| 81 | + }); |
| 82 | + |
| 83 | + test('contacts the GitHub API', async () => { |
| 84 | + const { getTemplateFiles } = require('../../src/templating/data'); |
| 85 | + |
| 86 | + const basePath = '/repos/twilio-labs/function-templates/contents/blank'; |
| 87 | + const templateContentScope = gitHubApi |
| 88 | + .get(`${basePath}?ref=master`) |
| 89 | + .reply(200, []); |
| 90 | + const result = await getTemplateFiles('blank'); |
| 91 | + |
| 92 | + expect(result).toEqual([]); |
| 93 | + expect(templateContentScope.isDone()).toBeTruthy(); |
| 94 | + }); |
| 95 | +}); |
0 commit comments