UNPKG

navy

Version:

Quick and powerful development environments using Docker and Docker Compose

134 lines (132 loc) 4.84 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _chai = require("chai"); var _sinon = _interopRequireDefault(require("sinon")); var _proxyquire = _interopRequireDefault(require("proxyquire")); /* eslint-env mocha */ describe('cli/wait-for-healthy', function () { let sandbox; let getNavyStub; let navyStub; let readlineStub; let consoleLogStub; let originalIsTTY; let waitForHealthyCli; let clock; beforeEach(function () { sandbox = _sinon.default.createSandbox(); navyStub = { name: 'env-1', waitForHealthy: sandbox.stub().resolves() }; getNavyStub = sandbox.stub().returns(navyStub); readlineStub = { clearLine: sandbox.stub(), cursorTo: sandbox.stub(), moveCursor: sandbox.stub() }; consoleLogStub = sandbox.stub(console, 'log'); originalIsTTY = process.stdout.isTTY; waitForHealthyCli = _proxyquire.default.noCallThru()('../wait-for-healthy', { '../': { getNavy: getNavyStub }, readline: readlineStub }); }); afterEach(function () { process.stdout.isTTY = originalIsTTY; if (clock) { clock.restore(); clock = null; } sandbox.restore(); }); describe('default export', function () { describe('non-TTY mode', function () { beforeEach(function () { process.stdout.isTTY = false; }); it('should call waitForHealthy with undefined when services are empty', async function () { await waitForHealthyCli([], { navy: 'env-1' }); (0, _chai.expect)(navyStub.waitForHealthy.calledOnce).to.equal(true); (0, _chai.expect)(navyStub.waitForHealthy.firstCall.args[0]).to.equal(undefined); }); it('should call waitForHealthy with the supplied services', async function () { await waitForHealthyCli(['web', 'api'], { navy: 'env-1' }); (0, _chai.expect)(navyStub.waitForHealthy.firstCall.args[0]).to.eql(['web', 'api']); }); it('should log "Now available" after the wait completes', async function () { await waitForHealthyCli([], { navy: 'env-1' }); (0, _chai.expect)(consoleLogStub.calledWith('Now available')).to.equal(true); }); }); describe('TTY mode', function () { beforeEach(function () { process.stdout.isTTY = true; clock = _sinon.default.useFakeTimers(); }); it('should call waitForHealthy with a callback for service health updates', async function () { const promise = waitForHealthyCli([], { navy: 'env-1' }); await promise; (0, _chai.expect)(navyStub.waitForHealthy.calledOnce).to.equal(true); (0, _chai.expect)(navyStub.waitForHealthy.firstCall.args[1]).to.be.a('function'); }); it('should print "Waiting" while no health status is reported', async function () { await waitForHealthyCli([], { navy: 'env-1' }); const printed = consoleLogStub.getCalls().map(c => c.args[0] || '').join('\n'); (0, _chai.expect)(printed).to.contain('Waiting'); }); it('should render a checkmark for healthy services and spinner for unhealthy', async function () { navyStub.waitForHealthy.callsFake(async (services, cb) => { cb([{ service: 'web', health: 'healthy' }, { service: 'api', health: 'starting' }]); }); await waitForHealthyCli([], { navy: 'env-1' }); const printed = consoleLogStub.getCalls().map(c => c.args[0] || '').join('\n'); (0, _chai.expect)(printed).to.contain('web'); (0, _chai.expect)(printed).to.contain('api'); }); it('should advance the spinner frame and redraw on the timer interval', async function () { navyStub.waitForHealthy.callsFake(() => new Promise(() => {})); const promise = waitForHealthyCli([], { navy: 'env-1' }); clock.tick(1000); (0, _chai.expect)(readlineStub.clearLine.called).to.equal(true); promise.catch(() => {}); }); it('should wrap spinner index back to zero when reaching the end of frames', async function () { navyStub.waitForHealthy.callsFake(() => new Promise(() => {})); waitForHealthyCli([], { navy: 'env-1' }).catch(() => {}); clock.tick(10000); (0, _chai.expect)(readlineStub.clearLine.called).to.equal(true); }); it('should pass services through to waitForHealthy', async function () { await waitForHealthyCli(['web'], { navy: 'env-1' }); (0, _chai.expect)(navyStub.waitForHealthy.firstCall.args[0]).to.eql(['web']); }); }); }); });