|
| 1 | +const {expect} = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const got = require('got'); |
| 4 | + |
| 5 | +const runTask = require('../../../lib/tasks/release-notes'); |
| 6 | + |
| 7 | +const stubbedGithubResponseWithRelevantFields = () => [{ |
| 8 | + tag_name: 'v4.0.1', |
| 9 | + body: '4.0.1 release notes' |
| 10 | +}, { |
| 11 | + tag_name: '3.42.2', |
| 12 | + body: '3.42.2 release notes' |
| 13 | +}]; |
| 14 | + |
| 15 | +describe('Unit: Tasks > Release Notes', function () { |
| 16 | + afterEach(function () { |
| 17 | + sinon.restore(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('Discovers releases for < 4.x', async function () { |
| 21 | + const stub = sinon.stub(got, 'get').resolves({body: stubbedGithubResponseWithRelevantFields()}); |
| 22 | + const task = {title: 'original'}; |
| 23 | + const ui = {log: sinon.stub()}; |
| 24 | + const context = {ui, version: '3.42.2'}; |
| 25 | + |
| 26 | + await runTask(context, task); |
| 27 | + |
| 28 | + expect(stub.calledOnce).to.be.true; |
| 29 | + expect(task.title).to.equal('Fetched release notes'); |
| 30 | + expect(ui.log.args[0]).to.deep.equal(['\n3.42.2 release notes\n', 'green']); |
| 31 | + }); |
| 32 | + |
| 33 | + it('Discovers release for >= 4.x', async function () { |
| 34 | + const stub = sinon.stub(got, 'get').resolves({body: stubbedGithubResponseWithRelevantFields()}); |
| 35 | + const task = {title: 'original'}; |
| 36 | + const ui = {log: sinon.stub()}; |
| 37 | + const context = {ui, version: '4.0.1'}; |
| 38 | + |
| 39 | + await runTask(context, task); |
| 40 | + |
| 41 | + expect(stub.calledOnce).to.be.true; |
| 42 | + expect(task.title).to.equal('Fetched release notes'); |
| 43 | + expect(ui.log.args[0]).to.deep.equal(['\n4.0.1 release notes\n', 'green']); |
| 44 | + }); |
| 45 | + |
| 46 | + it('Complains when there are no release notes', async function () { |
| 47 | + const stub = sinon.stub(got, 'get').resolves({body: stubbedGithubResponseWithRelevantFields()}); |
| 48 | + const task = {title: 'original'}; |
| 49 | + const ui = {log: sinon.stub()}; |
| 50 | + const context = {ui, version: '3.14.15'}; |
| 51 | + |
| 52 | + await runTask(context, task); |
| 53 | + |
| 54 | + expect(stub.calledOnce).to.be.true; |
| 55 | + expect(task.title).to.equal('Release notes were not found'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('Handles network errors', async function () { |
| 59 | + const stub = sinon.stub(got, 'get').rejects(new Error('What is this "GitHub" you speak of?')); |
| 60 | + const task = {title: 'original'}; |
| 61 | + const ui = {log: sinon.stub()}; |
| 62 | + const context = {ui, version: '3.14.15'}; |
| 63 | + |
| 64 | + await runTask(context, task); |
| 65 | + |
| 66 | + expect(stub.calledOnce).to.be.true; |
| 67 | + expect(task.title).to.equal('Unable to fetch release notes'); |
| 68 | + }); |
| 69 | +}); |
0 commit comments