From 218a1334bd8c938781bfbd5234148ff5243f0873 Mon Sep 17 00:00:00 2001 From: Naz Date: Wed, 21 Jul 2021 17:28:39 +0400 Subject: [PATCH] Added very basic unit test coverage to cover happy path import refs https://github.com/TryGhost/Team/issues/916 - These tests are nowhere close to perfect of even good. They barely pass "good enough" watermark... it's a start for new tests to be added easier in the future once the features are developed around CSV improrter --- .../fixtures/single-column-with-header.csv | 3 + ghost/members-importer/test/importer.test.js | 102 ++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 ghost/members-importer/test/fixtures/single-column-with-header.csv create mode 100644 ghost/members-importer/test/importer.test.js diff --git a/ghost/members-importer/test/fixtures/single-column-with-header.csv b/ghost/members-importer/test/fixtures/single-column-with-header.csv new file mode 100644 index 0000000000..28747a6d63 --- /dev/null +++ b/ghost/members-importer/test/fixtures/single-column-with-header.csv @@ -0,0 +1,3 @@ +email +jbloggs@example.com +test@example.com diff --git a/ghost/members-importer/test/importer.test.js b/ghost/members-importer/test/importer.test.js new file mode 100644 index 0000000000..11a6a026e4 --- /dev/null +++ b/ghost/members-importer/test/importer.test.js @@ -0,0 +1,102 @@ +// Switch these lines once there are useful utils +// const testUtils = require('./utils'); +require('./utils'); + +const path = require('path'); +const sinon = require('sinon'); +const MembersCSVImporter = require('..'); + +const csvPath = path.join(__dirname, '/fixtures/'); + +describe('Importer', function () { + describe('process', function () { + it('should import a CSV file', async function () { + const defaultProduct = { + id: 'default_product_id' + }; + + const membersApi = { + productRepository: { + list: async () => { + return { + data: [defaultProduct] + }; + } + }, + members: { + get: async () => { + return null; + }, + create: async (row) => { + return row; + } + } + }; + + const knexStub = { + transaction: sinon.stub().resolves({ + rollback: () => {}, + commit: () => {} + }) + }; + + const LabelModelStub = { + findOne: sinon.stub().resolves(null) + }; + + const importer = new MembersCSVImporter({ + storagePath: csvPath, + getTimezone: sinon.stub().returns('UTC'), + getMembersApi: () => membersApi, + sendEmail: sinon.stub(), + isSet: sinon.stub(), + addJob: sinon.stub(), + knex: knexStub, + urlFor: sinon.stub() + }); + + const result = await importer.process({ + pathToCSV: `${csvPath}/single-column-with-header.csv`, + headerMapping: {}, + importLabel: { + name: 'test import' + }, + user: { + email: 'test@example.com' + }, + LabelModel: LabelModelStub + }); + + should.exist(result.meta); + should.exist(result.meta.stats); + should.exist(result.meta.stats.imported); + result.meta.stats.imported.should.equal(2); + + should.exist(result.meta.stats.invalid); + should.equal(result.meta.import_label, null); + }); + }); + + describe('prepare', function () { + it('processes a basic valid import file for members', async function () { + const membersImporter = new MembersCSVImporter({ + storagePath: csvPath, + getTimezone: sinon.stub().returns('UTC'), + getMembersApi: sinon.stub(), + sendEmail: sinon.stub(), + isSet: sinon.stub(), + addJob: sinon.stub(), + knex: sinon.stub(), + urlFor: sinon.stub() + }); + + const result = await membersImporter.prepare(`${csvPath}/single-column-with-header.csv`); + + should.exist(result.id); + result.id.should.match(/\/members-importer\/test\/fixtures\/Members Import/); + + result.batches.should.equal(2); + should.exist(result.metadata); + }); + }); +});