|
| 1 | +const {expect} = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const proxyquire = require('proxyquire').noCallThru(); |
| 4 | + |
| 5 | +const {SystemError} = require('../../../lib/errors'); |
| 6 | + |
| 7 | +const modulePath = '../../../lib/commands/import'; |
| 8 | + |
| 9 | +describe('Unit: Commands > import', function () { |
| 10 | + it('throws error if importing a 0.x import into a > 1.x blog', async function () { |
| 11 | + const parseExport = sinon.stub().returns({version: '0.11.14'}); |
| 12 | + const ImportCommand = proxyquire(modulePath, {'../tasks/import': {parseExport}}); |
| 13 | + const getInstance = sinon.stub().returns({version: '3.0.0'}); |
| 14 | + |
| 15 | + const cmd = new ImportCommand({}, {getInstance}); |
| 16 | + |
| 17 | + try { |
| 18 | + await cmd.run({file: 'test-output.json'}); |
| 19 | + } catch (error) { |
| 20 | + expect(error).to.be.an.instanceof(SystemError); |
| 21 | + expect(error.message).to.include('can only be imported by Ghost v1.x versions'); |
| 22 | + expect(getInstance.calledOnce).to.be.true; |
| 23 | + expect(parseExport.calledOnceWithExactly('test-output.json')).to.be.true; |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + expect.fail('expected run to error'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('runs import task from v0.x to 1.x if blog is running', async function () { |
| 31 | + const parseExport = sinon.stub().returns({version: '0.11.14'}); |
| 32 | + const run = sinon.stub().resolves(); |
| 33 | + const importTask = sinon.stub().resolves({run}); |
| 34 | + const instance = { |
| 35 | + isRunning: sinon.stub().resolves(true), |
| 36 | + version: '1.0.0' |
| 37 | + }; |
| 38 | + |
| 39 | + const ImportCommand = proxyquire(modulePath, {'../tasks/import': {parseExport, importTask}}); |
| 40 | + const getInstance = sinon.stub().returns(instance); |
| 41 | + |
| 42 | + const cmd = new ImportCommand({ui: true}, {getInstance}); |
| 43 | + |
| 44 | + await cmd.run({file: 'test-output.json'}); |
| 45 | + expect(getInstance.calledOnce).to.be.true; |
| 46 | + expect(parseExport.calledOnceWithExactly('test-output.json')).to.be.true; |
| 47 | + expect(instance.isRunning.calledOnce).to.be.true; |
| 48 | + expect(importTask.calledOnceWithExactly({ui: true}, instance, 'test-output.json')).to.be.true; |
| 49 | + expect(run.calledOnce).to.be.true; |
| 50 | + }); |
| 51 | + |
| 52 | + it('runs import task from v1.x to any', async function () { |
| 53 | + const parseExport = sinon.stub().returns({version: '1.0.0'}); |
| 54 | + const run = sinon.stub().resolves(); |
| 55 | + const importTask = sinon.stub().resolves({run}); |
| 56 | + const instance = { |
| 57 | + isRunning: sinon.stub().resolves(true), |
| 58 | + version: '3.0.0' |
| 59 | + }; |
| 60 | + |
| 61 | + const ImportCommand = proxyquire(modulePath, {'../tasks/import': {parseExport, importTask}}); |
| 62 | + const getInstance = sinon.stub().returns(instance); |
| 63 | + |
| 64 | + const cmd = new ImportCommand({ui: true}, {getInstance}); |
| 65 | + |
| 66 | + await cmd.run({file: 'test-output.json'}); |
| 67 | + expect(getInstance.calledOnce).to.be.true; |
| 68 | + expect(parseExport.calledOnceWithExactly('test-output.json')).to.be.true; |
| 69 | + expect(instance.isRunning.calledOnce).to.be.true; |
| 70 | + expect(importTask.calledOnceWithExactly({ui: true}, instance, 'test-output.json')).to.be.true; |
| 71 | + expect(run.calledOnce).to.be.true; |
| 72 | + }); |
| 73 | + |
| 74 | + it('prompts to start if not running and throws if not confirmed', async function () { |
| 75 | + const parseExport = sinon.stub().returns({version: '1.0.0'}); |
| 76 | + const instance = { |
| 77 | + isRunning: sinon.stub().resolves(false), |
| 78 | + version: '3.0.0' |
| 79 | + }; |
| 80 | + const confirm = sinon.stub().resolves(false); |
| 81 | + |
| 82 | + const ImportCommand = proxyquire(modulePath, {'../tasks/import': {parseExport}}); |
| 83 | + const getInstance = sinon.stub().returns(instance); |
| 84 | + |
| 85 | + const cmd = new ImportCommand({confirm}, {getInstance}); |
| 86 | + |
| 87 | + try { |
| 88 | + await cmd.run({file: 'test-output.json'}); |
| 89 | + } catch (error) { |
| 90 | + expect(error).to.be.an.instanceof(SystemError); |
| 91 | + expect(error.message).to.include('not currently running'); |
| 92 | + expect(getInstance.calledOnce).to.be.true; |
| 93 | + expect(parseExport.calledOnceWithExactly('test-output.json')).to.be.true; |
| 94 | + expect(instance.isRunning.calledOnce).to.be.true; |
| 95 | + expect(confirm.calledOnce).to.be.true; |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + expect.fail('run should have errored'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('prompts to start if not running and starts if confirmed', async function () { |
| 103 | + const parseExport = sinon.stub().returns({version: '1.0.0'}); |
| 104 | + const runImport = sinon.stub().resolves(); |
| 105 | + const importTask = sinon.stub().resolves({run: runImport}); |
| 106 | + const instance = { |
| 107 | + isRunning: sinon.stub().resolves(false), |
| 108 | + checkEnvironment: sinon.stub(), |
| 109 | + start: sinon.stub().resolves(), |
| 110 | + version: '3.0.0' |
| 111 | + }; |
| 112 | + const confirm = sinon.stub().resolves(true); |
| 113 | + const run = sinon.stub().callsFake(fn => fn()); |
| 114 | + |
| 115 | + const ImportCommand = proxyquire(modulePath, {'../tasks/import': {parseExport, importTask}}); |
| 116 | + const getInstance = sinon.stub().returns(instance); |
| 117 | + |
| 118 | + const cmd = new ImportCommand({confirm, run}, {getInstance}); |
| 119 | + |
| 120 | + await cmd.run({file: 'test-output.json'}); |
| 121 | + |
| 122 | + expect(getInstance.calledOnce).to.be.true; |
| 123 | + expect(parseExport.calledOnceWithExactly('test-output.json')).to.be.true; |
| 124 | + expect(instance.isRunning.calledOnce).to.be.true; |
| 125 | + expect(confirm.calledOnce).to.be.true; |
| 126 | + expect(instance.checkEnvironment.calledOnce).to.be.true; |
| 127 | + expect(run.calledOnce).to.be.true; |
| 128 | + expect(instance.start.calledOnce).to.be.true; |
| 129 | + expect(importTask.calledOnceWithExactly({confirm, run}, instance, 'test-output.json')).to.be.true; |
| 130 | + expect(runImport.calledOnce).to.be.true; |
| 131 | + }); |
| 132 | +}); |
0 commit comments