forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunittests.js
More file actions
234 lines (187 loc) · 8.96 KB
/
unittests.js
File metadata and controls
234 lines (187 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, describe, it, expect, beforeEach, afterEach, waitsFor, runs, $, brackets, waitsForDone, waitsForFail */
define(function (require, exports, module) {
"use strict";
var SpecRunnerUtils = brackets.getModule("spec/SpecRunnerUtils");
var main = require("main"),
InlineDocsViewer = require("InlineDocsViewer"),
testCSS = require("text!unittest-files/test1.css"),
testHTML = require("text!unittest-files/test1.html"),
description = require("text!unittest-files/description.html");
describe("WebPlatformDocs", function () {
var testCSSInfo = SpecRunnerUtils.parseOffsetsFromText(testCSS),
testHTMLInfo = SpecRunnerUtils.parseOffsetsFromText(testHTML),
editor,
doc,
pos;
function queryInlineAtPos(info, offset, expectInline, expectedProperty) {
var widget = null,
promise;
runs(function () {
// set cursor position in editor
pos = info.offsets[offset];
editor.setSelection(pos);
// fetch inline editor
promise = main._inlineProvider(editor, pos);
if (expectInline) {
expect(promise).toBeTruthy();
}
if (promise) {
promise.done(function (result) {
widget = result;
});
if (expectInline) {
// expecting a valid CSS property
waitsForDone(promise, "WebPlatformDocs _inlineProvider", 1000);
} else {
// expecting an invalid css property
waitsForFail(promise, "WebPlatformDocs _inlineProvider", 1000);
}
}
});
runs(function () {
if (promise) {
if (expectInline) {
expect(widget).toBeTruthy();
expect(widget.$htmlContent.find(".css-prop-summary h1").text()).toBe(expectedProperty);
} else {
expect(widget).toBeNull();
}
}
});
}
describe("InlineDocsProvider database", function () {
it("should retrieve the CSS docs database", function () {
var json;
runs(function () {
main._getCSSDocs().done(function (result) {
json = result;
});
});
waitsFor(function () { return json !== undefined; }, "read css.json database", 5000);
runs(function () {
expect(Object.keys(json.PROPERTIES).length).toBeGreaterThan(0);
});
});
});
describe("InlineDocsProvider parsing in CSS", function () {
beforeEach(function () {
var mock = SpecRunnerUtils.createMockEditor(testCSSInfo.text, "css");
editor = mock.editor;
doc = mock.doc;
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(doc);
});
it("should open docs when the selection is on a CSS property", function () {
/* css property */
queryInlineAtPos(testCSSInfo, 1, true, "border");
/* css value */
queryInlineAtPos(testCSSInfo, 2, true, "border");
});
it("should not open docs when the selection is not on a CSS property", function () {
/* css selector */
queryInlineAtPos(testCSSInfo, 0, false);
/* css comment */
queryInlineAtPos(testCSSInfo, 5, false);
});
it("should not open docs for an invalid CSS property", function () {
/* css invalid property */
queryInlineAtPos(testCSSInfo, 3, false);
});
it("should open docs for a vendor-prefixed CSS property", function () {
/* css -webkit- prefixed property */
queryInlineAtPos(testCSSInfo, 6, true, "animation");
});
it("should not open docs for an invalid CSS property (looking like a vendor-prefixed one)", function () {
/* css property invalidly prefixed */
queryInlineAtPos(testCSSInfo, 7, false);
});
});
describe("InlineDocsProvider parsing in HTML", function () {
beforeEach(function () {
var mock = SpecRunnerUtils.createMockEditor(testHTMLInfo.text, "html");
editor = mock.editor;
doc = mock.doc;
});
afterEach(function () {
SpecRunnerUtils.destroyMockEditor(doc);
});
it("should open docs for CSS in a <style> block", function () {
queryInlineAtPos(testHTMLInfo, 0, true, "border");
});
it("should not open docs for inline style attributes", function () {
queryInlineAtPos(testHTMLInfo, 1, false);
});
});
describe("InlineDocsViewer", function () {
function createCssPropDetails(summary, url, valuesArr) {
var values = [],
details = {
SUMMARY: summary,
URL: url,
VALUES: values
};
valuesArr.forEach(function (value) {
values.push({
TITLE: value[0] || undefined,
DESCRIPTION: value[1] || undefined
});
});
return details;
}
it("should process all anchor tags", function () {
var prop = "my-css-prop",
url = "http://dev.brackets.io/wiki/css/properties/my-css-prop",
details = createCssPropDetails(
prop,
url,
[["normal", description]]
),
viewer = new InlineDocsViewer(prop, details),
$a,
title,
href,
$links = viewer.$htmlContent.find("a:not(.close)");
// 7 links in description.html, 1 "more info" link in template
expect($links.length).toBe(8);
$links.each(function (i, anchor) {
$a = $(anchor);
href = $a.attr("href");
if ($a.attr("data-expected")) {
// transform all URLs, see unittest-files/description.html
expect(href).toBe($a.attr("data-expected"));
} else {
// accont for "read more" URL
expect(href).toBe(url);
expect($a.hasClass("more-info"));
}
// all links should have a title
expect($a.attr("title")).toBe(href);
});
});
});
});
});