paradigm-core
Version:
Paradigm Core Modules
232 lines (191 loc) • 5.75 kB
JavaScript
const {
Migrations,
OrganizationsPlugin,
ApplicationsPlugin,
TestHelpers: {MockHTTPServer},
} = require('structure-core')
const pluginsList = require('../helpers/plugins')
const ChannelModel = require('../../model')
const server = new MockHTTPServer(pluginsList)
const orgTestApi = new OrganizationsPlugin.TestAPI(server)
const appTestApi = new ApplicationsPlugin.TestAPI(server)
/** @test {ChannelModel} */
describe('Channels ChannelModel', function() {
before(function() {
this.migration = new Migrations({
db: 'test',
items: {tables: [{
action: 'create',
table: 'digital_assets',
indexes: [
'applicationId',
'updatedAt'
]
}]},
plugins: pluginsList
})
return this.migration.process()
})
beforeEach(async function() {
const orgRes = await orgTestApi.create({
title: 'Org 1'
})
this.orgId = orgRes.body.pkg.id
const app1Res = await appTestApi.create(this.orgId, {
title: 'App 1'
})
this.app1Id = app1Res.body.pkg.id
const app2Res = await appTestApi.create(this.orgId, {
title: 'App 2'
})
this.app2Id = app2Res.body.pkg.id
this.channelModel = new ChannelModel({
organizationId: this.orgId,
applicationId: this.app1Id
})
})
afterEach(function() {
return this.migration.purge()
})
/** @test {ChannelModel#create} */
it('should create a channel', async function() {
const channel = await this.channelModel.create({
desc: 'Potions Channel',
slug: 'potions',
title: 'Potions',
organizationId: this.orgId,
applicationId: this.app1Id
})
expect(channel.title).to.equal('Potions')
expect(channel.slug).to.equal('potions')
expect(channel.desc).to.equal('Potions Channel')
expect(channel.applicationId).to.equal(this.app1Id)
expect(channel.organizationId).to.equal(this.orgId)
})
/** @test {ChannelModel#ofCategory} */
it('should get channels of category slug', async function() {
await this.channelModel.create({
desc: '',
slug: 'trending-1-app-1',
title: 'Trending 1 App 1',
organizationId: this.orgId,
applicationId: this.app1Id,
resources: [
{name: '', categorySlugs: ['trending']}
]
})
await this.channelModel.create({
desc: '',
slug: 'trending-2-app-1',
title: 'Trending 2 App 1',
organizationId: this.orgId,
applicationId: this.app1Id,
resources: [
{name: '', categorySlugs: ['trending']}
]
})
await this.channelModel.create({
desc: '',
slug: 'no-trending-app-1',
title: 'No Trending App 1',
organizationId: this.orgId,
applicationId: this.app1Id,
resources: [
{name: '', categorySlugs: ['funny']}
]
})
await this.channelModel.create({
desc: '',
slug: 'trending-1-app-2',
title: 'Trending 1 App 2',
organizationId: this.orgId,
applicationId: this.app2Id,
resources: [
{name: '', categorySlugs: ['trending']}
]
})
const res = await this.channelModel.ofCategory('trending')
expect(res.length).to.equal(2)
expect(res[0].slug).to.equal('trending-1-app-1')
expect(res[1].slug).to.equal('trending-2-app-1')
})
/** @test {ChannelModel#ofCategory} */
it('should return empty array when category slug has no channels', async function() {
const res = await this.channelModel.ofCategory('trending')
expect(res).to.deep.equal([])
})
/** @test {ChannelModel#ofDocument} */
it('should get channels of document', async function() {
const res = await server
.post(`/api/${process.env.API_VERSION}/categories`)
.set('organizationid', this.orgId)
.set('applicationid', this.app1Id)
.send({
desc: '',
title: 'Trending'
})
const categoryId = res.body.pkg.id
const res2 = await server
.post(`/api/${process.env.API_VERSION}/documents`)
.set('organizationid', this.orgId)
.set('applicationid', this.app1Id)
.send({
title: 'My Doc',
slug: 'my-doc',
fields: [
{
body: '50 ways to make up a title',
label: 'title',
type: 'text-input'
},
],
categoryIds: [categoryId],
organizationId: this.orgId
})
const documentId = res2.body.pkg.id
await this.channelModel.create({
desc: '',
slug: 'trending-1-app-1',
title: 'Trending 1 App 1',
organizationId: this.orgId,
applicationId: this.app1Id,
resources: [
{name: '', categorySlugs: ['trending']}
]
})
await this.channelModel.create({
desc: '',
slug: 'trending-2-app-1',
title: 'Trending 2 App 1',
organizationId: this.orgId,
applicationId: this.app1Id,
resources: [
{name: '', categorySlugs: ['trending']}
]
})
await this.channelModel.create({
desc: '',
slug: 'no-trending-app-1',
title: 'No Trending App 1',
organizationId: this.orgId,
applicationId: this.app1Id,
resources: [
{name: '', categorySlugs: ['funny']}
]
})
await this.channelModel.create({
desc: '',
slug: 'trending-1-app-2',
title: 'Trending 1 App 2',
organizationId: this.orgId,
applicationId: this.app2Id,
resources: [
{name: '', categorySlugs: ['trending']}
]
})
const res3 = await this.channelModel.ofDocument(documentId)
expect(res3.length).to.equal(2)
expect(res3[0].slug).to.equal('trending-1-app-1')
expect(res3[1].slug).to.equal('trending-2-app-1')
})
})