Skip to content

Commit edb80c7

Browse files
committed
add unit test and fix lint errors
1 parent a843d43 commit edb80c7

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

src/core.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Module} from "./module";
22
import {EVENT, RESOURCE_LOADING_TYPE} from "./enums";
33
import {IPageFragmentConfig, IPageLibAsset, IPageLibConfiguration} from "./types";
44
import {on} from "./decorators";
5-
import {AssetHelper} from "./assetHelper";
5+
import { AssetHelper } from "./assetHelper";
66

77
export class Core extends Module {
88
private static observer: IntersectionObserver | undefined;
@@ -56,11 +56,10 @@ export class Core extends Module {
5656
@on(EVENT.ON_PAGE_LOAD)
5757
static pageLoaded() {
5858
const onFragmentRenderAssets = Core.__pageConfiguration.assets.filter(asset => {
59-
const fragment = Core.__pageConfiguration.fragments.find(fragment => fragment.name === asset.fragment);
60-
if(fragment){
61-
return asset.loadMethod === RESOURCE_LOADING_TYPE.ON_PAGE_RENDER && fragment.attributes.if !== "true" && !asset.preLoaded;
59+
if(asset.loadMethod === RESOURCE_LOADING_TYPE.ON_PAGE_RENDER && !asset.preLoaded) {
60+
const fragment = Core.__pageConfiguration.fragments.find(fragment => fragment.name === asset.fragment);
61+
return fragment && fragment.attributes.if !== "true";
6262
}
63-
return asset.loadMethod === RESOURCE_LOADING_TYPE.ON_PAGE_RENDER && !asset.preLoaded;
6463
});
6564

6665
const scripts = Core.createLoadQueue(onFragmentRenderAssets);

src/modules/storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class Storage extends Module {
2020

2121
static async printApplicationCacheInfo() {
2222
const cacheNames = await caches.keys();
23-
let storageList: { [key: string]: any } = {
23+
const storageList: { [key: string]: any } = {
2424
total: 0
2525
};
2626

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface IPageFragmentConfig {
3232
name: string;
3333
chunked: boolean;
3434
clientAsync: boolean;
35-
attributes: { [name: string]: string };
35+
attributes: { [name: string]: string, if: string };
3636
source: string | undefined;
3737
}
3838

test/core.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import {JSDOM} from "jsdom";
33
import {PuzzleJs} from "../src/puzzle";
44
import {Core} from "../src/core";
55
import {createPageLibConfiguration} from "./mock";
6+
import sinon from "sinon";
7+
import {AssetHelper} from "../src/assetHelper";
68
import * as faker from "faker";
79
import {IPageLibAsset, IPageLibConfiguration, IPageLibDependency} from "../src/types";
810
import {RESOURCE_LOADING_TYPE, RESOURCE_TYPE} from "../src/enums";
911

12+
const sandbox = sinon.createSandbox();
13+
1014
declare global {
1115
interface Window {
1216
[key: string]: any;
@@ -25,6 +29,7 @@ declare var global: Global;
2529
describe('Module - Core', () => {
2630
beforeEach(() => {
2731
global.window = (new JSDOM(``, {runScripts: "outside-only"})).window;
32+
sandbox.verifyAndRestore();
2833
});
2934

3035
afterEach(() => {
@@ -164,4 +169,43 @@ describe('Module - Core', () => {
164169
expect(queue).to.deep.eq(
165170
[]);
166171
});
172+
173+
it('should create true load queue for js assets excluding conditional fragments', function () {
174+
const assets = [
175+
{
176+
name: 'bundle1',
177+
dependent: ['vendor1'],
178+
preLoaded: false,
179+
link: 'bundle1.js',
180+
fragment: 'test',
181+
loadMethod: RESOURCE_LOADING_TYPE.ON_PAGE_RENDER,
182+
type: RESOURCE_TYPE.JS
183+
}
184+
] as IPageLibAsset[];
185+
const dependencies = [
186+
{
187+
name: 'vendor1',
188+
link: 'vendor1.js',
189+
preLoaded: false
190+
}
191+
] as IPageLibDependency[];
192+
const config = {
193+
dependencies,
194+
assets,
195+
fragments: [{
196+
name: 'test',
197+
attributes: {
198+
if: "true"
199+
}
200+
}],
201+
page: 'page'
202+
} as IPageLibConfiguration;
203+
204+
const mockLoadJsSeries = sandbox.mock(AssetHelper);
205+
206+
Core.config(JSON.stringify(config));
207+
Core.pageLoaded();
208+
209+
mockLoadJsSeries.expects("loadJsSeries").calledWith([]);
210+
});
167211
});

0 commit comments

Comments
 (0)