// Dependencies
// =============================================================================
import axios from 'axios';
import createTestElms from './helpers/create-test-elms';
import getCss from '../src/get-css';
import { expect } from 'chai';
// Constants & Variables
// =============================================================================
const isIElte9 = document.all && !window.atob;
// Suite
// =============================================================================
describe('get-css', function() {
const fixtures = window.__FIXTURES__;
// Hooks
// -------------------------------------------------------------------------
// Conditionally include web component+polyfill to avoid errors in IE < 11
before(function() {
const hasWebComponentSupport = () => 'customElements' in window;
const isNotIELessThan11 = navigator.appVersion.indexOf('MSIE') === -1;
if (!hasWebComponentSupport() && isNotIELessThan11) {
console.log('*** Injected: Web Component Polyfill ***');
require('@webcomponents/webcomponentsjs/webcomponents-bundle.js');
}
if (hasWebComponentSupport()) {
console.log('*** Injected: Web Component ***');
require('./helpers/inject-test-component.js')();
}
});
// Remove and `);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(styleCss);
done();
}
});
});
// Tests: `);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(styleCss);
done();
}
});
});
it('returns CSS from single SVG
`);
getCss({
include: 'svg style',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(styleCss);
done();
}
});
});
it('returns CSS from multiple `.repeat(2));
const expected = styleCss.repeat(styleElms.length);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
it('returns CSS from multiple `.repeat(2));
const expected = fixtures['style2.out.css'].repeat(styleElms.length);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
it('returns CSS from multiple `.repeat(2));
const expected = fixtures['style3.out.css'].repeat(styleElms.length);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
it('returns CSS from multiple `.repeat(2));
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
cssText = cssText.replace(/\n/g, '');
expect(cssText).to.equal(expected);
done();
}
});
});
});
// Tests: CSS
// -------------------------------------------------------------------------
describe(' nodes', function() {
it('returns CSS from single node', function(done) {
const linkUrl = '/base/tests/fixtures/style1.css';
const expected = fixtures['style1.css'];
createTestElms(``);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
it('returns CSS from single node with data URI scheme', function(done) {
const encodedCSS = encodeURIComponent(fixtures['style1.css']);
const URIScheme = `data:text/css;charset=UTF-8,${encodedCSS}`;
const expected = fixtures['style1.css'];
createTestElms(``);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
it('returns CSS from single node via CORS', function(done) {
const linkProtocol = 'https:';
const linkUrl = `${linkProtocol}//cdn.jsdelivr.net/npm/get-css-data@1.0.0/tests/fixtures/style1.css`;
createTestElms(``);
// IE9 does not support CORS requests with different protocol
if (isIElte9 & location.protocol !== linkProtocol) {
getCss({
include: '[data-test]',
onError(xhr, node, url) {
done();
}
});
}
else {
axios.get(linkUrl)
.then(response => response.data)
.then(expected => {
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
}
});
it('returns CSS from multiple nodes', function(done) {
const linkUrl = '/base/tests/fixtures/style1.css';
const linkElms = createTestElms(``.repeat(2));
const expected = fixtures['style1.css'].repeat(linkElms.length);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
it('returns CSS from multiple nodes with data URI scheme', function(done) {
const encodedCSS = encodeURIComponent(fixtures['style1.css']);
const URIScheme = `data:text/css;charset=UTF-8,${encodedCSS}`;
const linkElms = createTestElms(``.repeat(2));
const expected = fixtures['style1.css'].repeat(linkElms.length);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
it('returns empty string from single node w/ empty stylesheet', function(done) {
const linkUrl = '/base/tests/fixtures/style-empty.css';
const expected = fixtures['style-empty.css'];
createTestElms(``);
getCss({
include: '[data-test]',
onError(xhr, node, url) {
console.log('Error', node, url);
},
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
it('returns CSS from multiple nodes with flat @import', function(done) {
const linkUrl = '/base/tests/fixtures/style2.css';
const linkElms = createTestElms(``.repeat(2));
const expected = fixtures['style2.out.css'].repeat(linkElms.length);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
it('returns CSS from multiple nodes with nested @import', function(done) {
const linkUrl = '/base/tests/fixtures/style3.css';
const linkElms = createTestElms(``.repeat(2));
const expected = fixtures['style3.out.css'].repeat(linkElms.length);
getCss({
include: '[data-test]',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(expected);
done();
}
});
});
});
// Tests: & `);
getCss({
include: '[data-test]',
exclude: 'link',
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(styleCss);
done();
}
});
});
it('options.filter', function(done) {
const linkUrl = '/base/tests/fixtures/style1.css';
const styleCss = '.keepme { color: red; }';
createTestElms(``);
createTestElms(``);
getCss({
include: '[data-test]',
filter : /keepme/,
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(styleCss);
done();
}
});
});
it('options.skipDisabled', function(done) {
const linkUrl = '/base/tests/fixtures/style1.css';
const styleCss = fixtures['style1.css'];
const testElms = createTestElms([
``,
``
]);
for (const sheet of document.styleSheets) {
sheet.disabled = true;
}
function step1() {
getCss({
include : '[data-test]',
skipDisabled: true,
onComplete(cssText, cssArray, nodeArray) {
expect(nodeArray.length, '1:nodeArray').to.equal(0);
expect(cssArray.length, '1:cssArray').to.equal(0);
expect(cssText, '1:cssText').to.equal('');
// Enable `.repeat(2));
let onSuccessCount = 0;
getCss({
include: '[data-test]',
onSuccess(cssText, node, url) {
onSuccessCount++;
return '!';
},
onComplete(cssText, cssArray, nodeArray) {
expect(cssText, 'return value').to.equal('!'.repeat(styleElms.length));
expect(onSuccessCount, 'onSuccess count').to.equal(styleElms.length);
done();
}
});
});
it('triggers onSuccess callback for each node', function(done) {
const linkUrl = '/base/tests/fixtures/style1.css';
const linkElms = createTestElms(``.repeat(2));
let onSuccessCount = 0;
getCss({
include: '[data-test]',
onSuccess(cssText, node, url) {
onSuccessCount++;
return '!';
},
onComplete(cssText, cssArray, nodeArray) {
expect(cssText, 'return value').to.equal('!'.repeat(linkElms.length));
expect(onSuccessCount, 'onSuccess count').to.equal(linkElms.length);
done();
}
});
});
it('filters CSS and nodes based on onSuccess() return value', function(done) {
const testElms = createTestElms([
'',
'',
'',
''
]);
getCss({
include: '[data-test]',
onSuccess(cssText, node, url) {
const returnVals = [false, null, 0, ''];
const nodeIndex = testElms.indexOf(node);
if (nodeIndex > -1) {
return returnVals[nodeIndex];
}
},
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal('');
expect(cssArray.length).to.equal(0);
expect(nodeArray.length).to.equal(0);
done();
}
});
});
it('modifies CSS text based on onSuccess() value', function(done) {
const linkUrl = '/base/tests/fixtures/style1.css';
const styleCss = fixtures['style1.css'];
const modifiedCss = '.modified { color: red; }';
createTestElms(``);
createTestElms(``);
getCss({
include: '[data-test]',
onSuccess(cssText, node, url) {
return modifiedCss;
},
onComplete(cssText, cssArray, nodeArray) {
expect(cssText).to.equal(modifiedCss.repeat(2));
done();
}
});
});
it('triggers onError callback for each @import 404 error', function(done) {
const styleCss = '@import "fail.css";';
const styleElms = createTestElms(``.repeat(3));
let onErrorCount = 0;
getCss({
include: '[data-test]',
onError(xhr, node, url) {
onErrorCount++;
},
onComplete(cssText, cssArray, nodeArray) {
expect(onErrorCount).to.equal(styleElms.length);
done();
}
});
});
it('triggers onError callback for each 404 error', function(done) {
const linkUrl = 'fail.css';
const linkElms = createTestElms(``.repeat(2));
let onErrorCount = 0;
getCss({
include: '[data-test]',
onError(xhr, node, url) {
onErrorCount++;
},
onComplete(cssText, cssArray, nodeArray) {
expect(onErrorCount).to.equal(linkElms.length);
done();
}
});
});
it('triggers onError callback for each invalid XMLHttpRequest.responseText', function(done) {
const linkUrl = '/base/tests/fixtures/404.html';
const linkElms = createTestElms(``.repeat(2));
let onErrorCount = 0;
getCss({
include: '[data-test]',
onError(xhr, node, url) {
onErrorCount++;
},
onComplete(cssText, cssArray, nodeArray) {
expect(onErrorCount).to.equal(linkElms.length);
done();
}
});
});
it('triggers onComplete callback with no matching