-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[STRF-9947] new helper earlyHint (#190)
- Loading branch information
1 parent
9f405b9
commit 7c6cf32
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
|
||
}); |