Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ala Hawash committed Mar 25, 2024
1 parent 4a4e467 commit 7dda326
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/mdctl-core/test/export_stream.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-underscore-dangle, max-len */
const assert = require('assert'),
sinon = require('sinon'),
ExportStream = require('../streams/export_stream'),
Expand All @@ -14,6 +14,7 @@ describe('ExportStream', () => {

it('should initialize with empty runtimes array and completed set to false', () => {
assert.deepStrictEqual(exportStream.runtimes, [])
assert.deepStrictEqual(exportStream.sectionsWithResources, [])
assert.strictEqual(exportStream.completed, false)
})

Expand All @@ -37,7 +38,7 @@ describe('ExportStream', () => {

exportStream._transform(chunk, null, () => {})

sinon.assert.calledWith(pushSpy, new ExportSection(chunk, chunk.object))
sinon.assert.calledWith(pushSpy, new ExportSection(chunk, chunk.object, exportStream.sectionsWithResources))
})

it('should transform and push StreamChunk when the object is "stream"', () => {
Expand Down
132 changes: 132 additions & 0 deletions packages/mdctl-core/test/section.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/* eslint-disable no-underscore-dangle */
const assert = require('assert'),
{ ExportSection } = require('../streams/section')

describe('ExportSection', () => {
let exportSection

describe('constructor', () => {
beforeEach(() => {
exportSection = new ExportSection({}, 'key', [])
})

it('should initialize with empty scriptFiles, extraFiles, templateFiles, resourcePaths, and id', () => {
assert.deepStrictEqual(exportSection.scriptFiles, [])
assert.deepStrictEqual(exportSection.extraFiles, [])
assert.deepStrictEqual(exportSection.templateFiles, [])
assert.deepStrictEqual(exportSection.resourcePaths, [])
assert.strictEqual(typeof exportSection.id, 'string')
})

it('should add itself to sectionsWithResources if it has resourceIds and sectionsWithResources is an array', () => {
const sectionsWithResources = [],
content = {
resourceId: 'resource1'
}
exportSection = new ExportSection(content, 'key', sectionsWithResources)

assert.strictEqual(sectionsWithResources.length, 1)
assert.strictEqual(sectionsWithResources[0], exportSection)
})

it('should not add itself to sectionsWithResources if it does not have resourceIds', () => {
const sectionsWithResources = [],
content = {}
exportSection = new ExportSection(content, 'key', sectionsWithResources)

assert.strictEqual(sectionsWithResources.length, 0)
})

it('should not add itself to sectionsWithResources if sectionsWithResources is not an array', () => {
const sectionsWithResources = {},
content = {
resourceId: 'resource1'
}
exportSection = new ExportSection(content, 'key', sectionsWithResources)

assert.strictEqual(sectionsWithResources.length, undefined)
})
})

describe('extractAssets', () => {
it('should add extraFiles to the exportSection if resource is found in sectionsWithResources #CTXAPI-2569', () => {
const chunks = [
{
name: 'Section 1 with resources',
object: 'env',
resource: 'env',
favicon: [
{
filename: 'favicon.ico',
name: 'content',
mime: 'image/x-icon',
object: 'facet',
resourceId: 'e6bd7abd-d7b8-42af-a5d8-52666fc44943'
}
],
},
{
name: 'Section 2 with resources',
logo: [
{
filename: 'medable_logo_sm.png',
name: 'content',
mime: 'image/png',
object: 'facet',
resourceId: '1d1de797-6a1e-4d7e-a3c8-b76f4a4b4df1'
},
{
filename: 'medable_logo_sm.png',
name: 'thumbnail',
mime: 'image/png',
object: 'facet',
resourceId: 'adfa4f86-f6e5-4f6c-b52f-98a55ba4815f'
}
],
object: 'env',
resource: 'env',
},
],

sectionsWithResources = [],
sections = []

chunks.forEach((chunk) => {
// eslint-disable-next-line no-new
sections.push(new ExportSection(chunk, chunk.object, sectionsWithResources))
})

// eslint-disable-next-line one-var
const facet = new ExportSection({
ETag: '2dd97d9de738211e48611dd03f2e0826',
base64: 'base64string',
mime: 'image/png',
name: 'content for section 1',
object: 'facet',
resource: 'favicon.ico',
resourceId: 'e6bd7abd-d7b8-42af-a5d8-52666fc44943'
}, 'facet', sectionsWithResources)

assert.strictEqual(facet.extraFiles.length, 0)

facet.extractAssets()

assert.strictEqual(facet.extraFiles.length, 1)
assert.deepStrictEqual(facet.extraFiles[0], {
name: 'favicon.ico',
ext: 'png',
url: undefined,
base64: 'base64string',
streamId: undefined,
path: undefined,
remoteLocation: false,
sectionId: sections[0].id,
sectionName: sections[0].name,
pathTo: '$.favicon[0].filePath',
ETag: '2dd97d9de738211e48611dd03f2e0826',
PathETag: '$.favicon[0].ETag'
})
})

})
})

0 comments on commit 7dda326

Please sign in to comment.