UNPKG

paradigm-core

Version:
249 lines (182 loc) 5.6 kB
const Highwinds = require('highwinds') const { r, Migrations, } = require('structure-core') const pluginsList = require('../helpers/plugins') const ParadigmHighwinds = require('../../index') const applicationId = '12345' const applicationId2 = '678910' /** @test {ParadigmHighwinds} */ describe('ParadigmHighwinds', function() { before(function() { this.timeout(10000) this.sandbox = sinon.sandbox.create() this.purgeByUrl = this.sandbox.spy(() => { return {id: '1'} }) this.purgeByTagSpy = this.sandbox.spy(() => { return {id: '2'} }) this.purgeByManyTagsSpy = this.sandbox.spy(() => { return {id: '3'} }) this.sandbox.stub(Highwinds.prototype, 'purgeByUrl').callsFake(this.purgeByUrl) this.sandbox.stub(Highwinds.prototype, 'purgeByTag').callsFake(this.purgeByTagSpy) this.sandbox.stub(Highwinds.prototype, 'purgeByManyTags').callsFake(this.purgeByManyTagsSpy) this.mockDate = new Date() this.sandbox.stub(r, 'now').callsFake(() => { return this.mockDate }) this.migration = new Migrations({ db: 'test', plugins: pluginsList }) return this.migration.process() }) beforeEach(function() { this.highwinds = new ParadigmHighwinds(applicationId, 'www.example.com') }) after(function() { this.sandbox.restore() }) afterEach(function() { this.purgeByUrl.resetHistory() this.purgeByTagSpy.resetHistory() this.purgeByManyTagsSpy.resetHistory() return this.migration.purge() }) /** @test {ParadigmHighwinds#purgeAll} */ it('should purge all', async function() { const res = await this.highwinds.purgeAll() expect(res).to.deep.equal({id: '1'}) expect(this.purgeByUrl).to.have.been.calledWith( '', { invalidateOnly: false, recursive: true } ) }) /** @test {ParadigmHighwinds#purgeUrl} */ it('should purge a url', async function() { const res = await this.highwinds.purgeUrl('/api') expect(res).to.deep.equal({id: '1'}) expect(this.purgeByUrl).to.have.been.calledWith( '/api', { invalidateOnly: false, recursive: false } ) }) /** @test {ParadigmHighwinds#purgeUrl} */ it('should purge a url recursively', async function() { const res = await this.highwinds.purgeUrl('/api', true) expect(res).to.deep.equal({id: '1'}) expect(this.purgeByUrl).to.have.been.calledWith( '/api', { invalidateOnly: false, recursive: true } ) }) /** @test {ParadigmHighwinds#purgeUrl} */ it('should purge a url invalidateOnly', async function() { const res = await this.highwinds.purgeUrl('/api', false, true) expect(res).to.deep.equal({id: '1'}) expect(this.purgeByUrl).to.have.been.calledWith( '/api', { invalidateOnly: true, recursive: false } ) }) /** @test {ParadigmHighwinds#updateLastPurgedAt} */ it('should update last purged at for all docs on an app', async function() { const updatedIds = [] const notUpdatedIds = [] // 2 published documents for (let i=0; i<5; i++) { const res = await r .table('documents') .insert({ applicationId, status: "published" }) updatedIds.push(res.generated_keys[0]) } // Unpublished const res1 = await r .table('documents') .insert({ applicationId, status: "draft" }) notUpdatedIds.push(res1.generated_keys[0]) // Different app const res2 = await r .table('documents') .insert({ applicationId: applicationId2, status: "published" }) notUpdatedIds.push(res2.generated_keys[0]) await this.highwinds.updateLastPurgedAt() for (const id of updatedIds) { const res = await r .table('documents') .get(id) expect(res.lastPurgedAt).to.deep.equal(this.mockDate) } for (const id of notUpdatedIds) { const res = await r .table('documents') .get(id) expect(res.lastPurgedAt).to.be.undefined } }) /** @test {ParadigmHighwinds#updateLastPurgedAt} */ it('should update last purged at for specific docs on an app', async function() { const res1 = await r .table('documents') .insert({ applicationId, status: "published" }) const doc1 = res1.generated_keys[0] const res2 = await r .table('documents') .insert({ applicationId, status: "published" }) const doc2 = res2.generated_keys[0] await this.highwinds.updateLastPurgedAt([doc1]) const res3 = await r .table('documents') .get(doc1) expect(res3.lastPurgedAt).to.deep.equal(this.mockDate) const res4 = await r .table('documents') .get(doc2) expect(res4.lastPurgedAt).to.be.undefined }) /** @test {ParadigmHighwinds#purgeByTag} */ it('should purge by tag', async function() { const res = await this.highwinds.purgeByTag('tag1', {recursive: true}) expect(res).to.deep.equal({id: '2'}) expect(this.purgeByTagSpy).to.have.been.calledWith('tag1', {recursive: true}) }) /** @test {ParadigmHighwinds#purgeByManyTags} */ it('should purge by many tags', async function() { const res = await this.highwinds.purgeByManyTags( ['tag1', 'tag2'], {recursive: true} ) expect(res).to.deep.equal({id: '3'}) expect(this.purgeByManyTagsSpy).to.have.been.calledWith(['tag1', 'tag2'], {recursive: true}) }) })