UNPKG

backport

Version:

A CLI tool that automates the process of backporting commits

111 lines 4.57 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const handlebars_1 = __importDefault(require("handlebars")); const getTitle_1 = require("./getTitle"); describe('getTitle', () => { const commits = [ { sourceBranch: 'main', sourcePullRequest: { title: 'My PR Title', number: 55, url: 'https://github.com/backport-org/different-merge-strategies/pull/55', }, sourceCommit: { sha: 'abcdefghi', message: 'My commit message (#55)', }, }, { sourcePullRequest: { number: 56, title: 'My PR Title', url: 'https://github.com/backport-org/different-merge-strategies/pull/56', }, sourceCommit: { sha: 'jklmnopqr', message: 'Another commit message (#56)', }, sourceBranch: 'main', }, ]; it('has the default title', () => { const options = {}; expect((0, getTitle_1.getTitle)({ options, commits, targetBranch: '7.x' })).toEqual('[7.x] My commit message (#55) | Another commit message (#56)'); }); it('renders title with the original PR title', () => { const options = { prTitle: '[{{targetBranch}}] {{sourcePullRequest.title}}', }; expect((0, getTitle_1.getTitle)({ options, commits, targetBranch: '7.x' })).toEqual('[7.x] My PR Title'); }); it('renders title using a specific commit', () => { const options = { prTitle: '[{{targetBranch}}] {{commits.0.sourcePullRequest.title}}', }; expect((0, getTitle_1.getTitle)({ options, commits, targetBranch: '7.x' })).toEqual('[7.x] My PR Title'); }); it('renders title using {{commitMessages}}', () => { const options = { prTitle: 'Branch: "{{targetBranch}}". Messages: {{commitMessages}}', }; expect((0, getTitle_1.getTitle)({ options, commits, targetBranch: '7.x' })).toEqual('Branch: "7.x". Messages: My commit message (#55) | Another commit message (#56)'); }); it('support backticks', () => { const options = { prTitle: '[{{targetBranch}}] {{commitMessages}}', }; const commits = [ { sourceBranch: 'main', sourcePullRequest: { title: 'My PR Title', number: 55, url: 'https://github.com/backport-org/different-merge-strategies/pull/55', }, sourceCommit: { sha: 'abcdefghi', message: 'My commit message with `backticks` (#55)', }, }, ]; expect((0, getTitle_1.getTitle)({ options, commits, targetBranch: '7.x' })).toEqual('[7.x] My commit message with `backticks` (#55)'); }); it('does not break on curly brackets', () => { const options = { prTitle: '[{{targetBranch}}] {{commitMessages}}', }; const commits = [ { sourceBranch: 'main', sourcePullRequest: { title: 'My PR Title', number: 55, url: 'https://github.com/backport-org/different-merge-strategies/pull/55', }, sourceCommit: { sha: 'abcdefghi', message: "```metadata={{ extraActionsColor: 'text',\r\n }}\r\n```", }, }, ]; expect(() => (0, getTitle_1.getTitle)({ options, commits, targetBranch: '7.x' })).not.toThrow(); }); it('should return default description if handlebars compilation fails', () => { const compileError = new Error('Simulated compile error'); // Stub Handlebars.compile to throw an error. const compileSpy = jest .spyOn(handlebars_1.default, 'compile') .mockImplementation(() => { throw compileError; }); const options = {}; expect((0, getTitle_1.getTitle)({ options, commits, targetBranch: '7.x' })).toMatchInlineSnapshot(`"[7.x] My commit message (#55) | Another commit message (#56)"`); // Restore the stubs. compileSpy.mockRestore(); }); }); //# sourceMappingURL=getTitle.test.js.map