Skip to content

Add title to component iframes #722

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ export default class Component {
};
}

/**
* get iframe title for component.
* @return {String} iframe title.
*/
get iframeTitle() {
return this.options.text && this.options.text.iframeTitle;
}

/**
* initializes component by creating model and rendering view.
* @param {Object} [data] - data to initialize model with.
Expand Down
4 changes: 4 additions & 0 deletions src/components/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ export default class Cart extends Component {
return `${this.props.client.config.storefrontAccessToken}.${this.props.client.config.domain}.checkoutId`;
}

get iframeTitle() {
return this.options.text.title;
}

imageForLineItem(lineItem) {
const imageSize = 180;
const imageOptions = {
Expand Down
7 changes: 7 additions & 0 deletions src/defaults/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const defaults = {
unitPriceAccessibilitySeparator: 'per',
regularPriceAccessibilityLabel: 'Regular price',
salePriceAccessibilityLabel: 'Sale price',
iframeTitle: 'Product buy button',
},
},
modalProduct: {
Expand Down Expand Up @@ -122,6 +123,7 @@ const defaults = {
buttonDestination: 'cart',
text: {
button: 'ADD TO CART',
iframeTitle: 'Product details modal buy button',
},
},
modal: {
Expand All @@ -147,6 +149,9 @@ const defaults = {
},
order: ['contents'],
templates: modalTemplates,
text: {
iframeTitle: 'Product details modal',
},
},
productSet: {
iframe: true,
Expand All @@ -173,6 +178,7 @@ const defaults = {
},
text: {
nextPageButton: 'Next page',
iframeTitle: 'Product collection buy buttons',
},
},
option: {
Expand Down Expand Up @@ -305,6 +311,7 @@ const defaults = {
},
text: {
title: 'cart',
iframeTitle: 'Cart toggle',
},
},
window: {
Expand Down
3 changes: 3 additions & 0 deletions src/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export default class iframe {
});
Object.keys(iframeAttrs).forEach((key) => this.el.setAttribute(key, iframeAttrs[key]));
this.el.setAttribute('name', config.name);
if (config.title) {
this.el.setAttribute('title', config.title);
}
this.styleTag = null;
}

Expand Down
1 change: 1 addition & 0 deletions src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class View {
googleFonts: this.component.googleFonts,
name: this.component.name,
width: this.component.options.layout === 'vertical' ? this.component.options.width : null,
title: this.component.iframeTitle,
});
this.iframe.addClass(this.className);
return this.iframe.load();
Expand Down
6 changes: 6 additions & 0 deletions test/unit/cart/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -1590,4 +1590,10 @@ describe('Cart class', () => {
viewSetFocusStub.restore();
});
});

describe('get iframeTitle', () => {
it('returns the title from the options text', () => {
assert.equal(cart.iframeTitle, cart.options.text.title);
});
});
});
6 changes: 6 additions & 0 deletions test/unit/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ describe('Component class', () => {
});
});
});

describe('iframeTitle', () => {
it('returns the iframeTitle from the options text', () => {
assert.equal(component.iframeTitle, component.options.text.iframeTitle);
});
});
});

describe('"private" methods', () => {
Expand Down
14 changes: 14 additions & 0 deletions test/unit/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe('Iframe class', () => {
constructorConfig = Object.assign({}, configObject, {
googleFonts: ['Arial', 'Calibri'],
width: '200px',
title: 'Iframe title',
});
createElementSpy = sinon.spy(document, 'createElement');
setWidthStub = sinon.stub(Iframe.prototype, 'setWidth');
Expand Down Expand Up @@ -137,6 +138,19 @@ describe('Iframe class', () => {
assert.equal(iframe.el.getAttribute('name'), constructorConfig.name);
});

it('sets element title to title in config if it exists', () => {
assert.equal(iframe.el.getAttribute('title'), constructorConfig.title);
});

it('does not set element title if it does not exist in the config', () => {
constructorConfig.title = null;
const setAttributeStub = sinon.stub(iframe.el, 'setAttribute');
iframe = new Iframe(parent, constructorConfig);
assert.neverCalledWith(setAttributeStub, 'title');
assert.equal(iframe.el.getAttribute('title'), null);
setAttributeStub.restore();
});

it('sets styleTag to null', () => {
iframe = new Iframe(parent, constructorConfig);
assert.equal(iframe.styleTag = null);
Expand Down
3 changes: 3 additions & 0 deletions test/unit/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ describe('View class', () => {
value: {
iframe: true,
manifest: ['product', 'option'],
text: {
iframeTitle: 'Iframe title',
},
},
});
});
Expand Down