Skip to content

Instantly share code, notes, and snippets.

@ZiiSolutions
Created July 21, 2017 09:34
Show Gist options
  • Save ZiiSolutions/0075b6119fdea1908e1942b44fb2b630 to your computer and use it in GitHub Desktop.
Save ZiiSolutions/0075b6119fdea1908e1942b44fb2b630 to your computer and use it in GitHub Desktop.
'use strict';
const mocha = require('mocha');
const chai = require('chai');
const chaiAsPromised = require("chai-as-promised");
const should = chai.should();
const sinon = require('sinon');
const expect = chai.expect;
const ninjsConverter = require('../../lib/converters/ninjs-converter');
const DEFAULT_MIME_TYPE = 'image/jpeg';
const DEFAULT_BASE_URL = 'https://image.assets.pressassociation.io';
const DEFAULT_SCHEME = "http://images-repository/v1/item";
const DEFAULT_REL = 'source';
chai.use(chaiAsPromised);
let sandbox;
describe('NinjsConverter', function () {
beforeEach(function (done) {
sandbox = sinon.sandbox.create();
done();
});
afterEach(function (done) {
sandbox.restore();
done();
});
it('ninjs-converter should exist', (done) => {
ninjsConverter.should.exist;
done();
});
describe('convertToNinjs()', function () {
it('should return promise', (done) => {
expect(ninjsConverter.convertToNinjs({})).to.be.a("Promise");
done();
});
it('should return promise with expected result', (done) => {
let promise = ninjsConverter.convertToNinjs({});
let expected = {
profile: 'asset',
type: 'picture',
pubstatus: 'usable',
};
promise.should.eventually
.deep.equal(expected)
.notify(done);
});
});
describe('_convertToNinjs()', function () {
it('should return an object', (done) => {
expect(ninjsConverter._convertToNinjs({}, {})).to.be.a("object");
done();
});
it('returned json profile field value should be asset', (done) => {
let json = ninjsConverter._convertToNinjs({}, {});
expect(json.profile).to.deep.equal('asset');
done();
});
it('when embargoed returned json pubstatus field value should be withheld', (done) => {
let json = ninjsConverter._convertToNinjs({embargoed: "1/1/12.12:20:88"}, {});
expect(json.pubstatus).to.deep.equal('withheld');
done();
});
it('when not embargoed returned json pubstatus field value should be usable', (done) => {
let json = ninjsConverter._convertToNinjs({someKey: "someLock"}, {});
expect(json.pubstatus).to.deep.equal('usable');
done();
});
it('returned json firstcreated field value should be the same as the input json', (done) => {
let json = ninjsConverter._convertToNinjs({firstcreated: '14/5/2017'}, {});
expect(json.firstcreated).to.deep.equal('14/5/2017');
done();
});
it('returned json firstcreated field value should be empty if input json does not have', (done) => {
let json = ninjsConverter._convertToNinjs({}, {});
expect(json.firstcreated).to.be.undefined;
done();
});
it('returned json versioncreated field value should the same as the input json', (done) => {
let json = ninjsConverter._convertToNinjs({versioncreated: '1'}, {});
expect(json.versioncreated).to.deep.equal('1');
done();
});
it('returned json versioncreated field value should be empty', (done) => {
let json = ninjsConverter._convertToNinjs({}, {});
expect(json.versioncreated).to.be.undefined;
done();
});
it('returned json byline field value should the same as the input json', (done) => {
let json = ninjsConverter._convertToNinjs({byline: 'snark'}, {});
expect(json.byline).to.deep.equal('snark');
done();
});
it('returned json byline field value should be empty', (done) => {
let json = ninjsConverter._convertToNinjs({smoke: 'no'}, {});
expect(json.byline).to.be.undefined;
done();
});
it('returned json description_text field value should the same as the input json', (done) => {
let json = ninjsConverter._convertToNinjs({description_text: 'This is description text'}, {});
expect(json.description_text).to.deep.equal('This is description text');
done();
});
it('returned json description_text field value should the same as the input json', (done) => {
let json = ninjsConverter._convertToNinjs({foo: 'moo'}, {});
expect(json.description_text).to.be.undefined;
done();
});
it('returned json description_html value should be as expected', (done) => {
let json = ninjsConverter._convertToNinjs({description_text: 'This is description text'}, {});
expect(json.description_html).to.deep.equal('<p>This is description text</p>', {});
done();
});
it('returned json description_html should be undefined', (done) => {
let json = ninjsConverter._convertToNinjs({foo: 'This is foo'}, {});
expect(json.description_html).to.be.undefined;
done();
});
it('returned json embargoed value should be as expected', (done) => {
let json = ninjsConverter._convertToNinjs({embargoed: '1/1/18.12:50:33'}, {});
expect(json.embargoed).to.deep.equal('1/1/18.12:50:33');
done();
});
it('returned json embargoed should be undefined', (done) => {
let json = ninjsConverter._convertToNinjs({foo: 'This is foo'}, {});
expect(json.embargoed).to.be.undefined;
done();
});
it('should return expected object array with correct fields', (done) => {
let expected =
[{
code: "2.30968278",
rel: DEFAULT_REL,
scheme: DEFAULT_SCHEME
}];
let actual = ninjsConverter._convertToNinjs({uri: "2.30968278"}, {});
expect(actual.object).to.deep.eql(expected);
done();
});
it('should return undefined object array when no uri present', (done) => {
let actual = ninjsConverter._convertToNinjs({}, {});
expect(actual.object).to.be.undefined;
done();
});
it('should return expected renditions with correct fields', (done) => {
const testJson = {
renditions: {
full: {
href: 'https://images.static.press.net/v2/image/production/7b75cd3ea6da28976Y2.jpg',
height: '80',
width: '60'
}
}
};
let expected = {
profile: 'asset',
type: 'picture',
pubstatus: 'usable',
renditions: {
original: {
href: DEFAULT_BASE_URL + '/v2/image/production/7b75cd3ea6da28976Y2.jpg',
mimeType: DEFAULT_MIME_TYPE,
height: '80',
width: '60'
}
}
};
let actual = ninjsConverter._convertToNinjs(testJson, {});
expect(actual).to.deep.eql(expected);
done();
});
it('should not return renditions', (done) => {
let result = ninjsConverter._convertToNinjs({}, {});
expect(result.renditions).to.be.undefined;
done();
});
it('should not return subject array', (done) => {
let result = ninjsConverter._convertToNinjs({foo: 'foo'}, {});
expect(result.subject).to.be.undefined;
done();
});
it('should return subject array as expected', (done) => {
let testJson = {
category: 'S',
supplemental_category: 'Rugby'
};
let expected = [
{
code: 'paservice:sport',
name: 'Sport',
profile: 'paservice',
rel: 'partOf',
scheme: 'http://content-repository/v1/subject'
},
{
code: 'paservice:sport:rugby-league',
name: 'Rugby-League',
profile: 'paservice',
rel: 'partOf',
scheme: 'http://content-repository/v1/subject'
},
{
code: 'paservice:sport:rugby-union',
name: 'Rugby-Union',
profile: 'paservice',
rel: 'partOf',
scheme: 'http://content-repository/v1/subject'
}
];
let result = ninjsConverter._convertToNinjs(testJson, {});
expect(result.subject).to.deep.eql(expected);
done();
});
it('should return subject array with topics and keywords', (done) => {
let testJson = {
original_reference: 'pa-news-20170418-172702-politics_may_172453.jpg',
category: 'S',
supplemental_category: 'Rugby'
};
let expected = [
{
code: 'paservice:sport',
name: 'Sport',
profile: 'paservice',
rel: 'partOf',
scheme: 'http://content-repository/v1/subject'
},
{
code: 'paservice:sport:rugby-league',
name: 'Rugby-League',
profile: 'paservice',
rel: 'partOf',
scheme: 'http://content-repository/v1/subject'
},
{
code: 'paservice:sport:rugby-union',
name: 'Rugby-Union',
profile: 'paservice',
rel: 'partOf',
scheme: 'http://content-repository/v1/subject'
},
{
code: "patopic:politics",
name: "politics",
profile: "patopic",
rel: "about",
scheme: "http://content-repository/v1/subject"
},
{
code: "pakeyword:may",
name: "may",
profile: "pakeyword",
rel: "about",
scheme: "http://content-repository/v1/subject"
}
];
let result = ninjsConverter._convertToNinjs(testJson, {});
expect(result.subject).to.deep.eql(expected);
done();
});
it('should return body html', (done) => {
let testJson = {
headline: 'some headline',
description_text: 'description text',
object: {
source: {
code: 'se12'
}
},
renditions: {
full: {
href: 'https://some.test/pathname.jpg',
height: '6000',
width: '9000'
}
}
};
let expected = '<!-- EMBED START Image { id: \\\"se12\\" } -->'
+ '<figure><img src=\\"https://image.assets.pressassociation.io/pathname.jpg\\" alt=\\"some headline\\" />'
+ '<figcaption>description text</figcaption></figure>'
+ '<!-- EMBED END Image { id: \\\"se12\\" } -->';
var result = ninjsConverter._convertToNinjs(testJson, {});
console.log(result);
expect(result.body_html).to.deep.eql(expected);
done();
});
it('body html should be undefined', (done) => {
let testJson = {
description_text: 'description text',
object: {
source: {
code: 'se12'
}
},
renditions: {
full: {
href: 'https://some.test/pathname.jpg'
}
}
};
var result = ninjsConverter._convertToNinjs(testJson, {});
expect(result.body_html).to.be.undefined;
done();
});
});
//TODO create test cases for entirety of the json when the entire json spec is made available
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment