Skip to content

Commit

Permalink
[STRF-9947] new helper earlyHint (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafa-avila-bc authored Aug 22, 2022
1 parent 9f405b9 commit 7c6cf32
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const helpersList = [
'toLowerCase',
'truncate',
'unless',
'earlyHint',
];

const deprecatedHelpersList = [
Expand Down
27 changes: 27 additions & 0 deletions helpers/earlyHint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const {addResourceHint} = require('./lib/resourceHints');

const factory = globals => {
return function (path, state) {
const options = arguments[arguments.length - 1];

const type = options.hash.type;
const cors = options.hash.cors;

addResourceHint(
globals,
path,
state,
type,
cors
);

return path;
}
};

module.exports = [{
name: 'earlyHint',
factory
}];
1 change: 1 addition & 0 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ describe('helper registration', () => {
'equals',
'getShortMonth',
'pick',
'earlyHint',
].sort();

expect(helpers.map(helper => helper.name).sort()).to.be.equal(expectedHelpers);
Expand Down
74 changes: 74 additions & 0 deletions spec/helpers/earlyHint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const Lab = require('lab'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
it = lab.it,
beforeEach = lab.beforeEach;
const {buildRenderer, testRunner} = require("../spec-helpers");
const {expect} = require("code");
const {
resourceHintAllowedTypes,
resourceHintAllowedStates,
resourceHintAllowedCors
} = require("../../helpers/lib/resourceHints");

function template(path, state, type, cors) {
type = type ? `type="${type}"` : '';
cors = cors ? `cors="${cors}"` : '';
return `{{ earlyHint "${path}" "${state}" ${type} ${cors} }}`;
}

function randommer(items) {
return () => items[Math.floor(Math.random() * items.length)];
}

const randomHintState = randommer(Object.values(resourceHintAllowedStates));
const randomHintType = randommer(Object.values(resourceHintAllowedTypes));
const randomCors = randommer(Object.values(resourceHintAllowedCors));

let renderer, runTests;

describe('earlyHint', () => {

beforeEach(done => {
renderer = buildRenderer();
runTests = testRunner({renderer});
done();
});

it('should create a resource hint with only required properties', done => {
const path = '/asset/theme.css'
const state = randomHintState();
const input = template(path, state);
runTests([
{
input,
output: path
}
], () => {
const hints = renderer.getResourceHints();
expect(hints).to.have.length(1);
expect(hints[0]).to.equals({src: path, state, cors: 'no'});
done();
});
});

it('should create a resource hint with all the properties', done => {
const path = '/asset/theme.css'
const type = randomHintType();
const state = randomHintState();
const cors = randomCors();
const input = template(path, state, type, cors);
runTests([
{
input,
output: path
}
], () => {
const hints = renderer.getResourceHints();
expect(hints).to.have.length(1);
expect(hints[0]).to.equals({src: path, state, type, cors});
done();
});
});

});

0 comments on commit 7c6cf32

Please sign in to comment.