Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit c0c62f0

Browse files
committed
Several standalone unit tests.
1 parent 4ee1ec3 commit c0c62f0

File tree

4 files changed

+294
-1
lines changed

4 files changed

+294
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"scripts": {
2727
"lint": "eslint --format=codeframe .",
28-
"test": "jasmine test/webpack3/index.js test/webpack4/index.js",
28+
"test": "jasmine test/unit/* test/webpack3/index.js test/webpack4/index.js",
2929
"coverage": "nyc npm run test",
3030
"clean": "rimraf build",
3131
"build": "npm run clean && babel --out-dir=build src"
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const createHtmlElementString = require('../../src/lib/create-html-element-string');
19+
20+
describe(`Error Conditions:`, function() {
21+
it(`should throw when called without an elementName`, function(done) {
22+
expect(
23+
() => createHtmlElementString({})
24+
).toThrowError('Please provide an element name.');
25+
26+
done();
27+
});
28+
29+
it(`should throw when called with an elementName matching \\W`, function(done) {
30+
expect(
31+
() => createHtmlElementString({elementName: 'Testing!'})
32+
).toThrowError('The element name contains invalid characters.');
33+
34+
done();
35+
});
36+
});
37+
38+
describe(`Defaults Tests:`, function() {
39+
it(`should support usage with just an elementName, and defaults for everything else`, function(done) {
40+
const elementString = createHtmlElementString({
41+
elementName: 'test',
42+
});
43+
44+
expect(elementString).toEqual('<test>');
45+
46+
done();
47+
});
48+
});
49+
50+
describe(`Attributes Tests:`, function() {
51+
it(`should support attributes without values`, function(done) {
52+
const elementString = createHtmlElementString({
53+
elementName: 'test',
54+
attributes: {
55+
one: '',
56+
two: '',
57+
}
58+
});
59+
60+
expect(elementString).toEqual('<test one two>');
61+
62+
done();
63+
});
64+
65+
it(`should support a mix of attributes with and without values`, function(done) {
66+
const elementString = createHtmlElementString({
67+
elementName: 'test',
68+
attributes: {
69+
one: '',
70+
two: '2'
71+
}
72+
});
73+
74+
expect(elementString).toEqual('<test one two="2">');
75+
76+
done();
77+
});
78+
79+
it(`should add the attributes sorted in alphanumeric order`, function(done) {
80+
const elementString = createHtmlElementString({
81+
elementName: 'test',
82+
attributes: {
83+
xyz: '3',
84+
abc: '1',
85+
def: '2'
86+
}
87+
});
88+
89+
expect(elementString).toEqual('<test abc="1" def="2" xyz="3">');
90+
91+
done();
92+
});
93+
94+
it(`should properly escape the attribute values as strings`, function(done) {
95+
const elementString = createHtmlElementString({
96+
elementName: 'test',
97+
attributes: {
98+
string: `Strings: '"\``
99+
}
100+
});
101+
102+
expect(elementString).toEqual('<test string="Strings: \'\\"`">');
103+
104+
done();
105+
});
106+
});
107+
108+
describe(`Closing Tag Tests:`, function() {
109+
it(`should add a closing tag when specified`, function(done) {
110+
const elementString = createHtmlElementString({
111+
elementName: 'test',
112+
closingTagRequired: true,
113+
});
114+
115+
expect(elementString).toEqual('<test></test>');
116+
117+
done();
118+
});
119+
});

test/unit/determine-as-value.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const determineAsValue = require('../../src/lib/determine-as-value');
19+
20+
describe(`Error Conditions:`, function() {
21+
it(`should throw when called without an href value`, function(done) {
22+
expect(
23+
() => determineAsValue({optionsAs: 'ignored'})
24+
).toThrowError(`The 'href' parameter was not provided.`);
25+
26+
done();
27+
});
28+
29+
it(`should throw when called with an invalid optionsAs value`, function(done) {
30+
expect(
31+
() => determineAsValue({href: '/', optionsAs: {}})
32+
).toThrowError(`The 'as' option isn't set to a recognized value: [object Object]`);
33+
34+
done();
35+
});
36+
});
37+
38+
describe(`OptionsAs Tests:`, function() {
39+
it(`should support passing in a string`, function(done) {
40+
const asValue = determineAsValue({href: '/', optionsAs: 'test'});
41+
42+
expect(asValue).toEqual('test');
43+
44+
done();
45+
});
46+
47+
it(`should support passing in a function`, function(done) {
48+
const asValue = determineAsValue({href: '/', optionsAs: (href) => href + 'test'});
49+
50+
expect(asValue).toEqual('/test');
51+
52+
done();
53+
});
54+
55+
it(`should support passing in undefined, when href ends with .css`, function(done) {
56+
const asValue = determineAsValue({href: '/test.css'});
57+
58+
expect(asValue).toEqual('style');
59+
60+
done();
61+
});
62+
63+
it(`should support passing in undefined, when href ends with .woff2`, function(done) {
64+
const asValue = determineAsValue({href: '/test.woff2'});
65+
66+
expect(asValue).toEqual('font');
67+
68+
done();
69+
});
70+
71+
it(`should support passing in undefined, when href ends with .js`, function(done) {
72+
const asValue = determineAsValue({href: '/test.js'});
73+
74+
expect(asValue).toEqual('script');
75+
76+
done();
77+
});
78+
79+
it(`should support passing in undefined, when href ends with anything else`, function(done) {
80+
const asValue = determineAsValue({href: '/test.ignored'});
81+
82+
expect(asValue).toEqual('script');
83+
84+
done();
85+
});
86+
});
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
const insertLinksIntoHead = require('../../src/lib/insert-links-into-head');
19+
20+
describe(`Edge Conditions:`, function() {
21+
it(`should throw when called with HTML lacking a </head> or <body>`, function(done) {
22+
const html = '<html></html>';
23+
expect(
24+
() => insertLinksIntoHead({html, links: ['<link>']})
25+
).toThrowError(`The HTML provided did not contain a </head> or a <body>:\n\n<html></html>`);
26+
27+
done();
28+
});
29+
30+
it(`should return the HTML as-is when there are no links`, function(done) {
31+
const html = '<html><body></body></html>';
32+
const updatedHtml = insertLinksIntoHead({html});
33+
34+
expect(updatedHtml).toEqual(html);
35+
36+
done();
37+
});
38+
});
39+
40+
describe(`Normal Conditions:`, function() {
41+
it(`should support inserting a single link prior to the </head>`, function(done) {
42+
const html = '<html><head></head><body></body></html>';
43+
const updatedHtml = insertLinksIntoHead({
44+
html,
45+
links: ['<link>'],
46+
});
47+
48+
expect(updatedHtml).toEqual(`<html><head><link>\n</head><body></body></html>`);
49+
50+
done();
51+
});
52+
53+
it(`should support inserting multiple links prior to the </head>`, function(done) {
54+
const html = '<html><head></head><body></body></html>';
55+
const updatedHtml = insertLinksIntoHead({
56+
html,
57+
links: ['<link1>', '<link2>'],
58+
});
59+
60+
expect(updatedHtml).toEqual(`<html><head><link1>\n<link2>\n</head><body></body></html>`);
61+
62+
done();
63+
});
64+
65+
it(`should support inserting a single link prior to the <body> when there is no </head>`, function(done) {
66+
const html = '<html><body></body></html>';
67+
const updatedHtml = insertLinksIntoHead({
68+
html,
69+
links: ['<link1>'],
70+
});
71+
72+
expect(updatedHtml).toEqual(`<html><head>\n<link1>\n</head>\n<body></body></html>`);
73+
74+
done();
75+
});
76+
77+
it(`should support inserting multiple links prior to the <body> when there is no </head>`, function(done) {
78+
const html = '<html><body></body></html>';
79+
const updatedHtml = insertLinksIntoHead({
80+
html,
81+
links: ['<link1>', '<link2>'],
82+
});
83+
84+
expect(updatedHtml).toEqual(`<html><head>\n<link1>\n<link2>\n</head>\n<body></body></html>`);
85+
86+
done();
87+
});
88+
});

0 commit comments

Comments
 (0)