2019-12-12 15:59:15 +03:00
|
|
|
const should = require('should');
|
2020-04-08 21:31:55 +03:00
|
|
|
const sinon = require('sinon');
|
2020-03-30 18:26:47 +03:00
|
|
|
const hbs = require('../../../core/frontend/services/themes/engine');
|
|
|
|
const helpers = require('../../../core/frontend/helpers');
|
2020-04-08 21:31:55 +03:00
|
|
|
const labs = require('../../../core/server/services/labs');
|
2019-12-12 15:59:15 +03:00
|
|
|
const configUtils = require('../../utils/configUtils');
|
|
|
|
|
|
|
|
describe('{{cancel_link}} helper', function () {
|
2020-04-08 21:31:55 +03:00
|
|
|
let labsStub;
|
2019-12-12 15:59:15 +03:00
|
|
|
before(function (done) {
|
|
|
|
hbs.express4({partialsDir: [configUtils.config.get('paths').helperTemplates]});
|
|
|
|
|
|
|
|
hbs.cachePartials(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-08 21:31:55 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
labsStub = sinon.stub(labs, 'isSet').returns(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2019-12-12 15:59:15 +03:00
|
|
|
const defaultLinkClass = /class="gh-subscription-cancel"/;
|
|
|
|
const defaultErrorElementClass = /class="gh-error gh-error-subscription-cancel"/;
|
|
|
|
const defaultCancelLinkText = /Cancel subscription/;
|
|
|
|
const defaultContinueLinkText = /Continue subscription/;
|
|
|
|
|
|
|
|
it('should throw if subscription data is incorrect', function () {
|
2019-12-20 10:50:08 +03:00
|
|
|
const runHelper = function (data) {
|
|
|
|
return function () {
|
|
|
|
helpers.cancel_link.call(data);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
runHelper('not an object').should.throw();
|
2020-04-08 21:31:55 +03:00
|
|
|
runHelper(function () { }).should.throw();
|
|
|
|
runHelper({}).should.throw();
|
|
|
|
runHelper({id: ''}).should.throw();
|
|
|
|
runHelper({cancel_at_period_end: ''}).should.throw();
|
2019-12-12 15:59:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can render cancel subscription link', function () {
|
|
|
|
const rendered = helpers.cancel_link.call({
|
|
|
|
id: 'sub_cancel',
|
|
|
|
cancel_at_period_end: false
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(defaultLinkClass);
|
|
|
|
rendered.string.should.match(/data-members-cancel-subscription="sub_cancel"/);
|
|
|
|
rendered.string.should.match(defaultCancelLinkText);
|
|
|
|
|
|
|
|
rendered.string.should.match(defaultErrorElementClass);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can render continue subscription link', function () {
|
|
|
|
const rendered = helpers.cancel_link.call({
|
|
|
|
id: 'sub_continue',
|
|
|
|
cancel_at_period_end: true
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(defaultLinkClass);
|
|
|
|
rendered.string.should.match(/data-members-continue-subscription="sub_continue"/);
|
|
|
|
rendered.string.should.match(defaultContinueLinkText);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can render custom link class', function () {
|
|
|
|
const rendered = helpers.cancel_link.call({
|
|
|
|
id: 'sub_cancel',
|
|
|
|
cancel_at_period_end: false
|
|
|
|
}, {
|
|
|
|
hash: {
|
|
|
|
class: 'custom-link-class'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(/custom-link-class/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can render custom error class', function () {
|
|
|
|
const rendered = helpers.cancel_link.call({
|
|
|
|
id: 'sub_cancel',
|
|
|
|
cancel_at_period_end: false
|
|
|
|
}, {
|
|
|
|
hash: {
|
|
|
|
errorClass: 'custom-error-class'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(/custom-error-class/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can render custom cancel subscription link attributes', function () {
|
|
|
|
const rendered = helpers.cancel_link.call({
|
|
|
|
id: 'sub_cancel',
|
|
|
|
cancel_at_period_end: false
|
|
|
|
}, {
|
|
|
|
hash: {
|
|
|
|
cancelLabel: 'custom cancel link text'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(/custom cancel link text/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can render custom continue subscription link attributes', function () {
|
|
|
|
const rendered = helpers.cancel_link.call({
|
|
|
|
id: 'sub_cancel',
|
|
|
|
cancel_at_period_end: true
|
|
|
|
}, {
|
|
|
|
hash: {
|
|
|
|
continueLabel: 'custom continue link text'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(/custom continue link text/);
|
|
|
|
});
|
2020-04-08 21:31:55 +03:00
|
|
|
|
|
|
|
it('is disabled if labs flag is not set', function () {
|
|
|
|
labsStub.returns(false);
|
|
|
|
|
|
|
|
const rendered = helpers.cancel_link.call({
|
|
|
|
id: 'sub_continue',
|
|
|
|
cancel_at_period_end: true
|
|
|
|
});
|
|
|
|
|
|
|
|
should.exist(rendered);
|
|
|
|
|
|
|
|
rendered.string.should.match(/^<script/);
|
|
|
|
rendered.string.should.match(/helper is not available/);
|
|
|
|
});
|
2019-12-12 15:59:15 +03:00
|
|
|
});
|