@@ -14,13 +14,39 @@ describe('basic-loader', function () {
1414 } ) ;
1515 } ) ;
1616
17+ beforeEach ( ( ) => {
18+ document . querySelectorAll ( '[src="foo.js"]' ) . forEach ( ( tag ) => tag . remove ( ) ) ;
19+ document . querySelectorAll ( '[src="bar.png"]' ) . forEach ( ( tag ) => tag . remove ( ) ) ;
20+ document . querySelectorAll ( '[href="baz.css"]' ) . forEach ( ( tag ) => tag . remove ( ) ) ;
21+ } ) ;
22+
1723 it ( 'should load javascript files' , async ( ) => {
1824 expect ( window . foo ) . to . be . undefined ;
1925 await load . js ( 'foo.js' ) ;
2026 expect ( window . foo ) . to . be . true ;
2127 delete window . foo ;
2228 } ) ;
2329
30+ it ( 'should not load javascript files more than once' , async ( ) => {
31+ await load . js ( 'foo.js' ) ;
32+ await load . js ( 'foo.js' ) ;
33+ expect ( document . querySelectorAll ( 'script[src="foo.js"]' ) . length ) . to . equal ( 1 ) ;
34+ delete window . foo ;
35+ } ) ;
36+
37+ it ( 'should work with preload/prefetch/... of javascript files' , async ( ) => {
38+ const link = document . createElement ( 'link' ) ;
39+ link . rel = 'preload' ;
40+ link . href = 'foo.js' ;
41+ link . as = 'script' ;
42+ document . head . appendChild ( link ) ;
43+
44+ expect ( window . foo ) . to . be . undefined ;
45+ await load . js ( 'foo.js' ) ;
46+ expect ( window . foo ) . to . be . true ;
47+ delete window . foo ;
48+ } ) ;
49+
2450 it ( 'should raise a promise error for non-existent javascript files' , ( done ) => {
2551 load . js ( 'foo-missing.js' ) . catch ( ( ) => done ( ) ) ;
2652 } ) ;
@@ -31,6 +57,24 @@ describe('basic-loader', function () {
3157 expect ( document . getElementsByTagName ( 'img' ) ) . to . have . length ( 1 ) ;
3258 } ) ;
3359
60+ it ( 'should not load image files more than once' , async ( ) => {
61+ await load . img ( 'bar.png' ) ;
62+ await load . img ( 'bar.png' ) ;
63+ expect ( document . querySelectorAll ( 'img[src="bar.png"]' ) . length ) . to . equal ( 1 ) ;
64+ } ) ;
65+
66+ it ( 'should work with preload/prefetch/... of image files' , async ( ) => {
67+ const link = document . createElement ( 'link' ) ;
68+ link . rel = 'preload' ;
69+ link . href = 'bar.png' ;
70+ link . as = 'image' ;
71+ document . head . appendChild ( link ) ;
72+
73+ expect ( document . getElementsByTagName ( 'img' ) . length ) . to . equal ( 0 ) ;
74+ await load . img ( 'bar.png' ) ;
75+ expect ( document . getElementsByTagName ( 'img' ) ) . to . have . length ( 1 ) ;
76+ } ) ;
77+
3478 it ( 'should raise a promise error for non-existent image files' , ( done ) => {
3579 load . img ( 'bar-missing.png' ) . catch ( ( ) => done ( ) ) ;
3680 } ) ;
@@ -42,6 +86,25 @@ describe('basic-loader', function () {
4286 expect ( getProp ( target , 'color' ) ) . to . equal ( 'rgb(255, 0, 0)' ) ;
4387 } ) ;
4488
89+ it ( 'should not load css files more than once' , async ( ) => {
90+ await load . css ( 'baz.css' ) ;
91+ await load . css ( 'baz.css' ) ;
92+ expect ( document . querySelectorAll ( 'link[href="baz.css"]' ) . length ) . to . equal ( 1 ) ;
93+ } ) ;
94+
95+ it ( 'should work with preload/prefetch/... of css files' , async ( ) => {
96+ const link = document . createElement ( 'link' ) ;
97+ link . rel = 'preload' ;
98+ link . href = 'baz.css' ;
99+ link . as = 'style' ;
100+ document . head . appendChild ( link ) ;
101+
102+ const target = document . getElementById ( 'css-target' ) ;
103+ expect ( getProp ( target , 'color' ) ) . to . equal ( 'rgb(0, 0, 0)' ) ; // phantom is terribly picky about props
104+ await load . css ( 'baz.css' ) ;
105+ expect ( getProp ( target , 'color' ) ) . to . equal ( 'rgb(255, 0, 0)' ) ;
106+ } ) ;
107+
45108 it ( 'should raise a promise error for non-existent css files' , ( done ) => {
46109 load . img ( 'baz-missing.css' ) . catch ( ( ) => done ( ) ) ;
47110 } ) ;
0 commit comments