mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-24 23:48:13 -05:00
Handle empty array of slack setting objects
no issue - ensure at least one SlackIntegration object exists in the slack setting when deserialising - remove blank SlackIntegration objects when serialising
This commit is contained in:
parent
2d2712ed89
commit
7b599ceacd
2 changed files with 34 additions and 6 deletions
|
@ -1,19 +1,25 @@
|
|||
/* eslint-disable camelcase */
|
||||
import SlackObject from 'ghost-admin/models/slack-integration';
|
||||
import Transform from 'ember-data/transform';
|
||||
import {A as emberA, isArray as isEmberArray} from '@ember/array';
|
||||
import {isArray as isEmberArray} from '@ember/array';
|
||||
import {isEmpty} from '@ember/utils';
|
||||
|
||||
export default Transform.extend({
|
||||
deserialize(serialized) {
|
||||
let slackObj, settingsArray;
|
||||
let settingsArray;
|
||||
try {
|
||||
settingsArray = JSON.parse(serialized) || [];
|
||||
} catch (e) {
|
||||
settingsArray = [];
|
||||
}
|
||||
|
||||
slackObj = settingsArray.map(itemDetails => SlackObject.create(itemDetails));
|
||||
return emberA(slackObj);
|
||||
if (isEmpty(settingsArray)) {
|
||||
settingsArray.push({url: ''});
|
||||
}
|
||||
|
||||
let slackObjs = settingsArray.map(itemDetails => SlackObject.create(itemDetails));
|
||||
|
||||
return slackObjs;
|
||||
},
|
||||
|
||||
serialize(deserialized) {
|
||||
|
@ -21,8 +27,9 @@ export default Transform.extend({
|
|||
if (isEmberArray(deserialized)) {
|
||||
settingsArray = deserialized.map((item) => {
|
||||
let url = (item.get('url') || '').trim();
|
||||
|
||||
return {url};
|
||||
if (url) {
|
||||
return {url};
|
||||
}
|
||||
}).compact();
|
||||
} else {
|
||||
settingsArray = [];
|
||||
|
|
|
@ -6,6 +6,7 @@ import {setupTest} from 'ember-mocha';
|
|||
|
||||
describe('Unit: Transform: slack-settings', function () {
|
||||
setupTest('transform:slack-settings', {});
|
||||
|
||||
it('deserializes settings json', function () {
|
||||
let transform = this.subject();
|
||||
let serialized = '[{"url":"http://myblog.com/blogpost1"}]';
|
||||
|
@ -16,6 +17,16 @@ describe('Unit: Transform: slack-settings', function () {
|
|||
expect(result[0].get('url')).to.equal('http://myblog.com/blogpost1');
|
||||
});
|
||||
|
||||
it('deserializes empty array', function () {
|
||||
let transform = this.subject();
|
||||
let serialized = '[]';
|
||||
let result = transform.deserialize(serialized);
|
||||
|
||||
expect(result.length).to.equal(1);
|
||||
expect(result[0]).to.be.instanceof(SlackIntegration);
|
||||
expect(result[0].get('url')).to.equal('');
|
||||
});
|
||||
|
||||
it('serializes array of Slack settings', function () {
|
||||
let transform = this.subject();
|
||||
let deserialized = emberA([
|
||||
|
@ -25,4 +36,14 @@ describe('Unit: Transform: slack-settings', function () {
|
|||
|
||||
expect(result).to.equal('[{"url":"http://myblog.com/blogpost1"}]');
|
||||
});
|
||||
|
||||
it('serializes empty SlackIntegration objects', function () {
|
||||
let transform = this.subject();
|
||||
let deserialized = emberA([
|
||||
SlackIntegration.create({url: ''})
|
||||
]);
|
||||
let result = transform.serialize(deserialized);
|
||||
|
||||
expect(result).to.equal('[]');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue