Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix page-specific placeholders when referenced with layout uris #1175

Merged
merged 2 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/decorators/placeholders.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Vue from 'vue';
import _ from 'lodash';
import store from '../core-data/store';
import { editAttr, placeholderAttr, placeholderProp, fieldProp, componentListProp, componentProp } from '../utils/references';
import { get } from '../core-data/groups';
import { isEmpty } from '../utils/comparators';
Expand Down Expand Up @@ -124,12 +125,17 @@ export function isGroupEmpty(fields, placeholder, path) {
/**
* test to see if a component property is empty
* @param {array|string} field (string means it's an alias to a page area)
* @param {object} storeOverride for testing
* @return {Boolean}
*/
function isComponentListEmpty(field) {
return _.isEmpty(field);
// todo: I'm not sure if this will work for page areas in layouts,
// so please test against that before releasing
function isComponentListEmpty(field, storeOverride) {
if (_.isString(field)) {
// list is in the page! fetch the page data
return _.isEmpty(_.get(storeOverride, `state.page.data['${field}']`, []));
} else {
// list is in the component / layout
return _.isEmpty(field);
}
}

/**
Expand All @@ -145,9 +151,10 @@ function isComponentPropEmpty(field) {
* determine if a placeholder is needed
* @param {string} uri
* @param {string} path to field/group
* @param {object} storeOverride for testing
* @return {Boolean}
*/
export function hasPlaceholder(uri, path) {
export function hasPlaceholder(uri, path, storeOverride = store) {
const group = get(uri, path),
fields = group.fields,
schema = group.schema,
Expand Down Expand Up @@ -177,7 +184,7 @@ export function hasPlaceholder(uri, path) {
} else if (isGroup) {
return isGroupEmpty(fields, placeholder, path);
} else if (isComponentList) {
return isComponentListEmpty(fields[path], uri, path);
return isComponentListEmpty(fields[path], storeOverride);
} else if (isComponentProp) {
return isComponentPropEmpty(fields[path]);
} else {
Expand Down
21 changes: 19 additions & 2 deletions lib/decorators/placeholders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,29 @@ describe('placeholders', () => {
});

it('returns true if empty component list', () => {
// note: this may not currently work for component lists in the layout that alias to page areas
// todo: check this and update if necessary
groups.get.returns({ fields: { bar: null }, schema: { [componentListProp]: true, [placeholderProp]: true } });
expect(fn('foo', 'bar')).to.equal(true);
});

it('returns false if non-empty component list', () => {
groups.get.returns({ fields: { bar: ['something'] }, schema: { [componentListProp]: true, [placeholderProp]: true } });
expect(fn('foo', 'bar')).to.equal(false);
});

it('returns true if empty page-specific component list', () => {
// note: this may not currently work for component lists in the layout that alias to page areas
// todo: check this and update if necessary
groups.get.returns({ fields: { bar: 'bar' }, schema: { [componentListProp]: true, [placeholderProp]: true } });
expect(fn('foo', 'bar', { state: { page: { data: { bar: [] }}}})).to.equal(true);
});

it('returns false if non-empty page-specific component list', () => {
// note: this may not currently work for component lists in the layout that alias to page areas
// todo: check this and update if necessary
groups.get.returns({ fields: { bar: 'bar' }, schema: { [componentListProp]: true, [placeholderProp]: true } });
expect(fn('foo', 'bar', { state: { page: { data: { bar: ['something'] }}}})).to.equal(false);
});

it('returns true if empty component prop', () => {
groups.get.returns({ fields: { bar: null }, schema: { [componentProp]: true, [placeholderProp]: true } });
expect(fn('foo', 'bar')).to.equal(true);
Expand Down