Skip to content

Commit

Permalink
Merge pull request #159 from contentful/fix/should-not-publish
Browse files Browse the repository at this point in the history
Respect shouldPublish flag when set to false
  • Loading branch information
TimBeyer authored Jan 2, 2019
2 parents b53ad66 + 721bd05 commit 45c64ec
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/lib/action/entry-derive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import EntryDerive from '../interfaces/entry-derive'
import { APIAction } from './action'
import { OfflineAPI } from '../offline-api'
import { ContentType } from '../entities/content-type'
import isDefined from '../utils/is-defined'

import Entry from '../entities/entry'
import * as _ from 'lodash'

Expand All @@ -22,7 +24,7 @@ class EntryDeriveAction extends APIAction {
this.derivedContentType = entryDerivation.derivedContentType
this.deriveEntryForLocale = entryDerivation.deriveEntryForLocale
this.identityKey = entryDerivation.identityKey
this.shouldPublish = entryDerivation.shouldPublish || true
this.shouldPublish = isDefined(entryDerivation.shouldPublish) ? entryDerivation.shouldPublish : true
}

async applyTo (api: OfflineAPI) {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/entities/entry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cloneDeep } from 'lodash'
import APIEntry from '../interfaces/api-entry'
import { isNullOrUndefined } from 'util'
import isDefined from '../utils/is-defined'

class Entry {
private _id: string
Expand Down Expand Up @@ -64,7 +64,7 @@ class Entry {
}

get isPublished () {
return !isNullOrUndefined(this._publishedVersion)
return isDefined(this._publishedVersion)
}

get publishedVersion () {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/utils/is-defined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isDefined<T> (value: T | undefined | null): value is T {
return value !== undefined && value !== null
}
61 changes: 61 additions & 0 deletions test/unit/lib/actions/entry-derive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,67 @@ describe('Entry Derive', function () {
expect(updateEntryWithLinkFields.ownerRef['en-US'].sys.id).to.eq(batches[0].requests[0].data.sys.id); // id of linked object is same as id of target object
});

it('respects shouldPublish', async function () {
const action = new EntryDeriveAction('dog', {
derivedContentType: 'owner',
from: ['owner'],
toReferenceField: 'ownerRef',
derivedFields: ['firstName', 'lastName'],
identityKey: async (fromFields) => {
return fromFields.owner['en-US'].toLowerCase().replace(' ', '-');
},
shouldPublish: false,
deriveEntryForLocale: async (inputFields, locale) => {
if (locale !== 'en-US') {
return;
}
const [firstName, lastName] = inputFields.owner[locale].split(' ');
return {
firstName,
lastName
};
}
});

const contentTypes = new Map();
contentTypes.set('dog', new ContentType({
sys: {
id: 'dog'
},
fields: [{
name: 'ownerRef',
id: 'ownerRef',
type: 'Symbol'
}]
})
);

const entries = [
new Entry(makeApiEntry({
id: '246',
contentTypeId: 'dog',
version: 1,
fields: {
owner: {
'en-US': 'john doe'
}
}
}))
];

const api = new OfflineApi(contentTypes, entries, ['en-US']);
api.startRecordingRequests(null);
await action.applyTo(api);
api.stopRecordingRequests();
const batches = await api.getRequestBatches();
expect(batches[0].requests.length).to.eq(2);

expect(batches[0].requests[0].data.sys.publishedVersion).to.eql(undefined);
expect(batches[0].requests[0].data.sys.version).to.eql(0);
expect(batches[0].requests[1].data.sys.publishedVersion).to.eql(undefined);
expect(batches[0].requests[1].data.sys.version).to.eql(1);
});

it('derives an entry from n to n', async function () {
const action = new EntryDeriveAction('dog', {
derivedContentType: 'owner',
Expand Down

0 comments on commit 45c64ec

Please sign in to comment.