Extracted Image Importer test suite

refs https://github.com/TryGhost/Toolbox/issues/523
refs 3119a5cc4c

- The "index.test.js" is doing too much, has to be broken down to allow things to move into new packages
This commit is contained in:
Naz 2023-03-01 16:48:38 +08:00
parent 4c2ff0539d
commit e14520115a
No known key found for this signature in database
2 changed files with 59 additions and 54 deletions

View File

@ -0,0 +1,59 @@
const _ = require('lodash');
const sinon = require('sinon');
const storage = require('../../../../../../core/server/adapters/storage');
const ImageImporter = require('../../../../../../core/server/data/importer/importers/image');
describe('ImageImporter', function () {
it('has the correct interface', function () {
ImageImporter.type.should.eql('images');
ImageImporter.preProcess.should.be.instanceof(Function);
ImageImporter.doImport.should.be.instanceof(Function);
});
it('does preprocess posts, users and tags correctly', function () {
let inputData = require('../../../../../utils/fixtures/import/import-data-1.json');
let outputData = ImageImporter.preProcess(_.cloneDeep(inputData));
inputData = inputData.data.data;
outputData = outputData.data.data;
inputData.posts[0].markdown.should.not.containEql('/content/images/my-image.png');
inputData.posts[0].html.should.not.containEql('/content/images/my-image.png');
outputData.posts[0].markdown.should.containEql('/content/images/my-image.png');
outputData.posts[0].html.should.containEql('/content/images/my-image.png');
inputData.posts[0].markdown.should.not.containEql('/content/images/photos/cat.jpg');
inputData.posts[0].html.should.not.containEql('/content/images/photos/cat.jpg');
outputData.posts[0].markdown.should.containEql('/content/images/photos/cat.jpg');
outputData.posts[0].html.should.containEql('/content/images/photos/cat.jpg');
inputData.posts[0].feature_image.should.eql('/images/my-image.png');
outputData.posts[0].feature_image.should.eql('/content/images/my-image.png');
inputData.tags[0].feature_image.should.eql('/images/my-image.png');
outputData.tags[0].feature_image.should.eql('/content/images/my-image.png');
inputData.users[0].profile_image.should.eql('/images/my-image.png');
inputData.users[0].cover_image.should.eql('/images/photos/cat.jpg');
outputData.users[0].profile_image.should.eql('/content/images/my-image.png');
outputData.users[0].cover_image.should.eql('/content/images/photos/cat.jpg');
});
it('does import the images correctly', function () {
const inputData = require('../../../../../utils/fixtures/import/import-data-1.json');
const storageApi = {
save: sinon.stub().returns(Promise.resolve())
};
const storageSpy = sinon.stub(storage, 'getStorage').callsFake(function () {
return storageApi;
});
ImageImporter.doImport(inputData.images).then(function () {
storageSpy.calledOnce.should.be.true();
storageApi.save.calledTwice.should.be.true();
});
});
});

View File

@ -696,58 +696,4 @@ describe('Importer', function () {
inputData.data.data.users[0].should.eql(outputData.data.data.users[0]);
});
});
describe('ImageImporter', function () {
it('has the correct interface', function () {
ImageImporter.type.should.eql('images');
ImageImporter.preProcess.should.be.instanceof(Function);
ImageImporter.doImport.should.be.instanceof(Function);
});
it('does preprocess posts, users and tags correctly', function () {
let inputData = require('../../../../utils/fixtures/import/import-data-1.json');
let outputData = ImageImporter.preProcess(_.cloneDeep(inputData));
inputData = inputData.data.data;
outputData = outputData.data.data;
inputData.posts[0].markdown.should.not.containEql('/content/images/my-image.png');
inputData.posts[0].html.should.not.containEql('/content/images/my-image.png');
outputData.posts[0].markdown.should.containEql('/content/images/my-image.png');
outputData.posts[0].html.should.containEql('/content/images/my-image.png');
inputData.posts[0].markdown.should.not.containEql('/content/images/photos/cat.jpg');
inputData.posts[0].html.should.not.containEql('/content/images/photos/cat.jpg');
outputData.posts[0].markdown.should.containEql('/content/images/photos/cat.jpg');
outputData.posts[0].html.should.containEql('/content/images/photos/cat.jpg');
inputData.posts[0].feature_image.should.eql('/images/my-image.png');
outputData.posts[0].feature_image.should.eql('/content/images/my-image.png');
inputData.tags[0].feature_image.should.eql('/images/my-image.png');
outputData.tags[0].feature_image.should.eql('/content/images/my-image.png');
inputData.users[0].profile_image.should.eql('/images/my-image.png');
inputData.users[0].cover_image.should.eql('/images/photos/cat.jpg');
outputData.users[0].profile_image.should.eql('/content/images/my-image.png');
outputData.users[0].cover_image.should.eql('/content/images/photos/cat.jpg');
});
it('does import the images correctly', function () {
const inputData = require('../../../../utils/fixtures/import/import-data-1.json');
const storageApi = {
save: sinon.stub().returns(Promise.resolve())
};
const storageSpy = sinon.stub(storage, 'getStorage').callsFake(function () {
return storageApi;
});
ImageImporter.doImport(inputData.images).then(function () {
storageSpy.calledOnce.should.be.true();
storageApi.save.calledTwice.should.be.true();
});
});
});
});