| 1 |
www |
1 |
"use strict";
|
|
|
2 |
|
|
|
3 |
describe("loadUtils:", function() {
|
|
|
4 |
|
|
|
5 |
beforeEach(function() {
|
|
|
6 |
intlSetup();
|
|
|
7 |
// must be in markup for utils loaded handler to work
|
|
|
8 |
input = $("<input>").appendTo("body");
|
|
|
9 |
});
|
|
|
10 |
|
|
|
11 |
afterEach(function() {
|
|
|
12 |
intlTeardown();
|
|
|
13 |
});
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
describe("calling loadUtils before init plugin", function() {
|
|
|
18 |
|
|
|
19 |
var url = "build/js/utils.js?v=1",
|
|
|
20 |
resolved = false;
|
|
|
21 |
|
|
|
22 |
beforeEach(function(done) {
|
|
|
23 |
var promise = window.intlTelInputGlobals.loadUtils(url);
|
|
|
24 |
promise.then(function() {
|
|
|
25 |
resolved = true;
|
|
|
26 |
done();
|
|
|
27 |
});
|
|
|
28 |
});
|
|
|
29 |
|
|
|
30 |
afterEach(function() {
|
|
|
31 |
resolved = false;
|
|
|
32 |
});
|
|
|
33 |
|
|
|
34 |
it("injects the script", function() {
|
|
|
35 |
expect($("script.iti-load-utils")).toExist();
|
|
|
36 |
expect($("script.iti-load-utils").attr("src")).toEqual(url);
|
|
|
37 |
});
|
|
|
38 |
|
|
|
39 |
it("does resolve the promise", function() {
|
|
|
40 |
expect(resolved).toEqual(true);
|
|
|
41 |
});
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
describe("then init plugin with utilsScript option", function() {
|
|
|
46 |
|
|
|
47 |
var resolved2 = false;
|
|
|
48 |
|
|
|
49 |
beforeEach(function(done) {
|
|
|
50 |
iti = window.intlTelInput(input[0], {
|
|
|
51 |
utilsScript: "some/other/url/ok",
|
|
|
52 |
});
|
|
|
53 |
iti.promise.then(function() {
|
|
|
54 |
resolved2 = true;
|
|
|
55 |
});
|
|
|
56 |
setTimeout(done);
|
|
|
57 |
});
|
|
|
58 |
|
|
|
59 |
afterEach(function() {
|
|
|
60 |
resolved2 = false;
|
|
|
61 |
});
|
|
|
62 |
|
|
|
63 |
it("does not inject another script", function() {
|
|
|
64 |
expect($("script.iti-load-utils").length).toEqual(1);
|
|
|
65 |
expect($("script.iti-load-utils").attr("src")).toEqual(url);
|
|
|
66 |
});
|
|
|
67 |
|
|
|
68 |
it("does resolve the promise immediately", function() {
|
|
|
69 |
expect(resolved2).toEqual(true);
|
|
|
70 |
});
|
|
|
71 |
|
|
|
72 |
});
|
|
|
73 |
|
|
|
74 |
});
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
describe("init plugin with utilsScript option, but force windowLoaded=false so it wont fire", function() {
|
|
|
79 |
|
|
|
80 |
var url2 = "build/js/utils.js?v=2",
|
|
|
81 |
resolved = false;
|
|
|
82 |
|
|
|
83 |
beforeEach(function(done) {
|
|
|
84 |
window.intlTelInputGlobals.documentReady = () => false;
|
|
|
85 |
iti = window.intlTelInput(input[0], {
|
|
|
86 |
utilsScript: "some/other/url/ok",
|
|
|
87 |
});
|
|
|
88 |
iti.promise.then(function() {
|
|
|
89 |
resolved = true;
|
|
|
90 |
});
|
|
|
91 |
waitForUtilsRequest(done);
|
|
|
92 |
});
|
|
|
93 |
|
|
|
94 |
afterEach(function() {
|
|
|
95 |
resolved = false;
|
|
|
96 |
});
|
|
|
97 |
|
|
|
98 |
it("does not inject the script", function() {
|
|
|
99 |
expect($("script.iti-load-utils")).not.toExist();
|
|
|
100 |
});
|
|
|
101 |
|
|
|
102 |
it("does not resolve the promise", function() {
|
|
|
103 |
expect(resolved).toEqual(false);
|
|
|
104 |
});
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
describe("calling loadUtils", function() {
|
|
|
109 |
|
|
|
110 |
beforeEach(function(done) {
|
|
|
111 |
window.intlTelInputGlobals.loadUtils(url2);
|
|
|
112 |
waitForUtilsRequest(done);
|
|
|
113 |
});
|
|
|
114 |
|
|
|
115 |
it("does inject the script", function() {
|
|
|
116 |
expect($("script.iti-load-utils")).toExist();
|
|
|
117 |
});
|
|
|
118 |
|
|
|
119 |
it("does resolve the promise", function() {
|
|
|
120 |
expect(resolved).toEqual(true);
|
|
|
121 |
});
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
|
125 |
describe("then init another plugin instance with utilsScript option", function() {
|
|
|
126 |
|
|
|
127 |
var iti2,
|
|
|
128 |
input2,
|
|
|
129 |
resolved2 = false;
|
|
|
130 |
|
|
|
131 |
beforeEach(function(done) {
|
|
|
132 |
input2 = $("<input>").appendTo("body");
|
|
|
133 |
iti2 = window.intlTelInput(input2[0], {
|
|
|
134 |
utilsScript: "test/url/three/utils.js",
|
|
|
135 |
});
|
|
|
136 |
iti2.promise.then(function() {
|
|
|
137 |
resolved2 = true;
|
|
|
138 |
});
|
|
|
139 |
setTimeout(done);
|
|
|
140 |
});
|
|
|
141 |
|
|
|
142 |
afterEach(function() {
|
|
|
143 |
iti2.destroy();
|
|
|
144 |
input2.remove();
|
|
|
145 |
iti2 = input2 = null;
|
|
|
146 |
});
|
|
|
147 |
|
|
|
148 |
it("does not inject another script", function() {
|
|
|
149 |
expect($("script.iti-load-utils").length).toEqual(1);
|
|
|
150 |
expect($("script.iti-load-utils").attr("src")).toEqual(url2);
|
|
|
151 |
});
|
|
|
152 |
|
|
|
153 |
it("does resolve the promise immediately", function() {
|
|
|
154 |
expect(resolved2).toEqual(true);
|
|
|
155 |
});
|
|
|
156 |
|
|
|
157 |
});
|
|
|
158 |
|
|
|
159 |
});
|
|
|
160 |
|
|
|
161 |
});
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
describe("fake window.load event then init plugin with utilsScript", function() {
|
|
|
166 |
|
|
|
167 |
var url3 = "build/js/utils.js?v=3";
|
|
|
168 |
|
|
|
169 |
beforeEach(function(done) {
|
|
|
170 |
window.intlTelInputGlobals.documentReady = () => true;
|
|
|
171 |
iti = window.intlTelInput(input[0], {
|
|
|
172 |
utilsScript: url3,
|
|
|
173 |
});
|
|
|
174 |
// wait for the request to finish so we dont interfere with other tests
|
|
|
175 |
iti.promise.finally(done);
|
|
|
176 |
});
|
|
|
177 |
|
|
|
178 |
it("injects the script", function() {
|
|
|
179 |
expect($("script.iti-load-utils")).toExist();
|
|
|
180 |
});
|
|
|
181 |
|
|
|
182 |
it("then calling loadUtils does not inject another script", function() {
|
|
|
183 |
window.intlTelInputGlobals.loadUtils("this/is/a/test");
|
|
|
184 |
expect($("script.iti-load-utils").length).toEqual(1);
|
|
|
185 |
expect($("script.iti-load-utils").attr("src")).toEqual(url3);
|
|
|
186 |
});
|
|
|
187 |
|
|
|
188 |
});
|
|
|
189 |
|
|
|
190 |
});
|