prism-ad-campaigns
Version:
Prism Ad Campaigns
140 lines (104 loc) • 3.94 kB
JavaScript
const Migrations = require('structure-migrations')
const MockHTTPServer = require('../helpers/mock-http-server')
const pluginsList = require('../helpers/plugins')
const OrgTestAPI = require('structure-organizations/test/helpers/test-api').default
const AppTestAPI = require('structure-applications/test/helpers/test-api').default
const UserTestAPI = require('structure-users/test/helpers/test-api').default
const DocTestAPI = require('paradigm-documents/test/helpers/test-api-documents')
const CreateAdCampaignJob = require('../../src/jobs/create-ad-campaign')
const server = new MockHTTPServer()
const orgTestApi = new OrgTestAPI(server)
const appTestApi = new AppTestAPI(server)
const userTestApi = new UserTestAPI(server)
const docTestApi = new DocTestAPI(server)
/** @test {hooks} */
describe('Hooks', function() {
before(function() {
this.timeout(10000)
this.sandbox = sinon.sandbox.create()
this.createQueueSpy = this.sandbox.spy()
this.sandbox.stub(CreateAdCampaignJob.prototype, 'queue', this.createQueueSpy)
this.migration = new Migrations({
db: 'test',
plugins: pluginsList
})
this.createDoc = async (pkg) => {
const docRes = await docTestApi.create(
this.orgId,
this.appId,
Object.assign({
title: 'Harry Potter Facts',
slug: 'harry-potter-facts',
fields: [
{
body: 'Why Harry Potter should have used his invisibility cloak more...',
label: 'title',
type: 'text-input'
},
],
categoryIds: [],
organizationId: this.orgId,
userId: this.userId
}, pkg)
)
this.doc = docRes.body.pkg
}
return this.migration.process()
})
beforeEach(async function() {
const orgRes = await orgTestApi.create({
title: 'Forbidden Children'
})
this.orgId = orgRes.body.pkg.id
const appRes = await appTestApi.create(this.orgId, {
desc: '',
title: 'App 45',
host: 'app45.com'
})
this.appId = appRes.body.pkg.id
const userRes = await userTestApi.create(this.orgId, this.appId, {
organizationId: this.orgId,
username: 'tom3',
email: 'voldemort3@horcruxes.r.us',
password: 'c4ntf1ndmyc4v3br0',
timezone: 'Europe/Madrid'
})
this.userId = userRes.body.pkg.id
})
after(function() {
this.sandbox.restore()
})
afterEach(function() {
this.createQueueSpy.reset()
return this.migration.purge()
})
/** @test {Hooks#publish} */
it('should queue not create update job after create route called with draft status', async function() {
await this.createDoc()
expect(this.createQueueSpy.called).to.be.false
})
/** @test {Hooks#publish} */
it('should queue create update job after create route called with published status', async function() {
await this.createDoc({status: 'published'})
expect(this.createQueueSpy).to.have.been.calledOnce
expect(this.createQueueSpy.args[0][0].document.id).to.equal(this.doc.id)
})
/** @test {Hooks#publish} */
it('should queue create update job after publish route called', async function() {
await this.createDoc()
const res = await docTestApi.publish(this.orgId, this.appId, this.doc.id)
expect(res.status).to.equal(201)
expect(this.createQueueSpy).to.have.been.calledOnce
expect(this.createQueueSpy.args[0][0].document.id).to.equal(this.doc.id)
})
/** @test {Hooks#updatePublish} */
it('should queue create update job after doc published via update', async function() {
await this.createDoc()
const res = await docTestApi.update(this.orgId, this.appId, this.doc.id, {
status: 'published'
})
expect(res.status).to.equal(200)
expect(this.createQueueSpy).to.have.been.calledOnce
expect(this.createQueueSpy.args[0][0].document.id).to.equal(this.doc.id)
})
})