paradigm-facebook-ia
Version:
Paradigm Facebook Instant Articles
201 lines (144 loc) • 5.97 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 CreateUpdateArticleJob = require('../../src/jobs/create-update-article')
const DeleteArticleJob = require('../../src/jobs/delete-article')
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.createUpdateArticleQueueSpy = this.sandbox.spy()
this.deleteArticleQueueSpy = this.sandbox.spy()
this.sandbox.stub(CreateUpdateArticleJob.prototype, 'queue', this.createUpdateArticleQueueSpy)
this.sandbox.stub(DeleteArticleJob.prototype, 'queue', this.deleteArticleQueueSpy)
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.docId = docRes.body.pkg.id
}
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.createUpdateArticleQueueSpy.reset()
this.deleteArticleQueueSpy.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.createUpdateArticleQueueSpy.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.createUpdateArticleQueueSpy).to.have.been.calledOnce
expect(this.createUpdateArticleQueueSpy.args[0][0].documentId).to.equal(this.docId)
})
/** @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.docId)
expect(res.status).to.equal(201)
expect(this.createUpdateArticleQueueSpy).to.have.been.calledOnce
expect(this.createUpdateArticleQueueSpy.args[0][0].documentId).to.equal(this.docId)
})
/** @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.docId, {
status: 'published'
})
expect(res.status).to.equal(200)
expect(this.createUpdateArticleQueueSpy).to.have.been.calledOnce
expect(this.createUpdateArticleQueueSpy.args[0][0].documentId).to.equal(this.docId)
})
/** @test {Hooks#unpublish} */
it('should queue delete job after doc unpublish route called', async function() {
await this.createDoc()
const res = await docTestApi.unpublish(this.orgId, this.appId, this.docId)
expect(res.status).to.equal(201)
expect(this.deleteArticleQueueSpy).to.have.been.calledOnce
expect(this.deleteArticleQueueSpy.args[0][0].documentId).to.equal(this.docId)
})
/** @test {Hooks#updateUnpublish} */
it('should queue delete job after doc unpublished via update', async function() {
await this.createDoc()
const res = await docTestApi.update(this.orgId, this.appId, this.docId, {
status: 'published'
})
expect(res.status).to.equal(200)
this.deleteArticleQueueSpy.reset()
const res2 = await docTestApi.update(this.orgId, this.appId, this.docId, {
status: 'deleted'
})
expect(res2.status).to.equal(200)
expect(this.deleteArticleQueueSpy).to.have.been.calledOnce
expect(this.deleteArticleQueueSpy.args[0][0].documentId).to.equal(this.docId)
})
/** @test {Hooks#delete} */
it('should queue delete job after doc delete route called', async function() {
await this.createDoc()
const res = await docTestApi.update(this.orgId, this.appId, this.docId, {
status: 'published'
})
expect(res.status).to.equal(200)
this.deleteArticleQueueSpy.reset()
const res2 = await docTestApi.delete(this.orgId, this.appId, this.docId)
expect(res2.status).to.equal(200)
expect(this.deleteArticleQueueSpy).to.have.been.calledOnce
expect(this.deleteArticleQueueSpy.args[0][0].documentId).to.equal(this.docId)
})
})