Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadScript() takes a css option to load stylesheet by URL #86

Merged
merged 1 commit into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- `loadScript()` takes a `css` option to load stylesheet by URL
### Changed
### Fixed
### Removed
Expand Down
6 changes: 5 additions & 1 deletion src/esri-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function handleScriptError(script, callback) {
// interfaces
export interface ILoadScriptOptions {
url?: string;
// TODO: css?: string;
css?: string;
dojoConfig?: { [propName: string]: any };
}

Expand Down Expand Up @@ -136,6 +136,10 @@ export function loadScript(options: ILoadScriptOptions = {}): Promise<HTMLScript
reject(new Error(`The ArcGIS API for JavaScript is already loaded.`));
} else {
// this is the first time attempting to load the API
if (options.css) {
// load the css before loading the script
loadCss(options.css);
}
if (options.dojoConfig) {
// set dojo configuration parameters before loading the script
window['dojoConfig'] = options.dojoConfig;
Expand Down
23 changes: 23 additions & 0 deletions test/esri-loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('esri-loader', function () {
// trigger the onload event listeners
el.dispatchEvent(new Event('load'));
});
spyOn(document.head, 'appendChild');
esriLoader.loadScript()
.then((script) => {
// hold onto script element for assertions below
Expand All @@ -97,6 +98,9 @@ describe('esri-loader', function () {
it('should not have set dojoConfig', function () {
expect(window.dojoConfig).not.toBeDefined();
});
it('should not have called loadCss', function () {
expect(document.head.appendChild.calls.any()).toBeFalsy();
});
});
describe('with different API version', function () {
var scriptEl;
Expand All @@ -121,6 +125,25 @@ describe('esri-loader', function () {
expect(window.dojoConfig).not.toBeDefined();
});
});
describe('with css option', function () {
var cssUrl = 'https://js.arcgis.com/4.6/esri/css/main.css';
beforeAll(function (done) {
spyOn(document.body, 'appendChild').and.callFake(function (el) {
// trigger the onload event listeners
el.dispatchEvent(new Event('load'));
});
spyOn(document.head, 'appendChild').and.stub();
esriLoader.loadScript({
css: cssUrl
})
.then((script) => {
done();
});
});
it('should have called loadCss with the url', function () {
expect(document.head.appendChild.calls.argsFor(0)[0].href).toEqual(cssUrl);
});
});
describe('with dojoConfig option', function () {
var dojoConfig = {
async: true,
Expand Down