2016-02-13 02:31:26 +08:00
|
|
|
/* global Int8Array:true */
|
2016-06-15 02:31:51 +08:00
|
|
|
import {
|
|
|
|
emailValid,
|
2016-06-16 01:49:57 +08:00
|
|
|
extractDomainFromUrl,
|
2016-06-15 02:31:51 +08:00
|
|
|
avatarUrl,
|
|
|
|
getRawSize,
|
|
|
|
avatarImg,
|
|
|
|
defaultHomepage,
|
2017-11-10 03:45:19 +08:00
|
|
|
setDefaultHomepage,
|
2016-06-15 02:31:51 +08:00
|
|
|
caretRowCol,
|
2018-01-13 10:43:49 +08:00
|
|
|
setCaretPosition,
|
2019-12-03 00:48:40 +08:00
|
|
|
fillMissingDates,
|
|
|
|
getCodeBlocks,
|
|
|
|
inCodeBlock
|
2018-06-15 23:03:24 +08:00
|
|
|
} from "discourse/lib/utilities";
|
2016-06-15 02:31:51 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.module("lib:utilities");
|
2013-06-20 03:06:23 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("emailValid", assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.ok(
|
|
|
|
emailValid("Bob@example.com"),
|
|
|
|
"allows upper case in the first part of emails"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
emailValid("bob@EXAMPLE.com"),
|
|
|
|
"allows upper case in the email domain"
|
|
|
|
);
|
2013-06-20 03:06:23 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("extractDomainFromUrl", assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
extractDomainFromUrl("http://meta.discourse.org:443/random"),
|
|
|
|
"meta.discourse.org",
|
|
|
|
"extract domain name from url"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
extractDomainFromUrl("meta.discourse.org:443/random"),
|
|
|
|
"meta.discourse.org",
|
|
|
|
"extract domain regardless of scheme presence"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
extractDomainFromUrl("http://192.168.0.1:443/random"),
|
|
|
|
"192.168.0.1",
|
|
|
|
"works for IP address"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
extractDomainFromUrl("http://localhost:443/random"),
|
|
|
|
"localhost",
|
|
|
|
"works for localhost"
|
|
|
|
);
|
2016-06-16 01:49:57 +08:00
|
|
|
});
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("avatarUrl", assert => {
|
2016-06-15 02:31:51 +08:00
|
|
|
var rawSize = getRawSize;
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.blank(avatarUrl("", "tiny"), "no template returns blank");
|
|
|
|
assert.equal(
|
|
|
|
avatarUrl("/fake/template/{size}.png", "tiny"),
|
|
|
|
"/fake/template/" + rawSize(20) + ".png",
|
|
|
|
"simple avatar url"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
avatarUrl("/fake/template/{size}.png", "large"),
|
|
|
|
"/fake/template/" + rawSize(45) + ".png",
|
|
|
|
"different size"
|
|
|
|
);
|
2013-07-19 00:03:09 +08:00
|
|
|
});
|
|
|
|
|
2015-03-20 20:53:56 +08:00
|
|
|
var setDevicePixelRatio = function(value) {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (Object.defineProperty && !window.hasOwnProperty("devicePixelRatio")) {
|
2015-08-12 00:27:07 +08:00
|
|
|
Object.defineProperty(window, "devicePixelRatio", { value: 2 });
|
2015-03-20 20:53:56 +08:00
|
|
|
} else {
|
|
|
|
window.devicePixelRatio = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("avatarImg", assert => {
|
2014-05-27 12:40:46 +08:00
|
|
|
var oldRatio = window.devicePixelRatio;
|
2015-03-20 20:53:56 +08:00
|
|
|
setDevicePixelRatio(2);
|
2014-05-27 12:40:46 +08:00
|
|
|
|
2013-08-14 04:08:29 +08:00
|
|
|
var avatarTemplate = "/path/to/avatar/{size}.png";
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
avatarImg({ avatarTemplate: avatarTemplate, size: "tiny" }),
|
|
|
|
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar'>",
|
|
|
|
"it returns the avatar html"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
avatarImg({
|
|
|
|
avatarTemplate: avatarTemplate,
|
|
|
|
size: "tiny",
|
|
|
|
title: "evilest trout"
|
|
|
|
}),
|
|
|
|
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar' title='evilest trout'>",
|
|
|
|
"it adds a title if supplied"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
avatarImg({
|
|
|
|
avatarTemplate: avatarTemplate,
|
|
|
|
size: "tiny",
|
|
|
|
extraClasses: "evil fish"
|
|
|
|
}),
|
|
|
|
"<img alt='' width='20' height='20' src='/path/to/avatar/40.png' class='avatar evil fish'>",
|
|
|
|
"it adds extra classes if supplied"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.blank(
|
|
|
|
avatarImg({ avatarTemplate: "", size: "tiny" }),
|
|
|
|
"it doesn't render avatars for invalid avatar template"
|
|
|
|
);
|
2014-05-27 12:40:46 +08:00
|
|
|
|
2015-03-20 20:53:56 +08:00
|
|
|
setDevicePixelRatio(oldRatio);
|
2013-07-22 08:39:17 +08:00
|
|
|
});
|
2013-08-24 00:48:29 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("defaultHomepage", assert => {
|
2013-12-31 01:46:18 +08:00
|
|
|
Discourse.SiteSettings.top_menu = "latest|top|hot";
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
defaultHomepage(),
|
|
|
|
"latest",
|
|
|
|
"default homepage is the first item in the top_menu site setting"
|
|
|
|
);
|
2017-11-10 03:45:19 +08:00
|
|
|
var meta = document.createElement("meta");
|
|
|
|
meta.name = "discourse_current_homepage";
|
|
|
|
meta.content = "hot";
|
|
|
|
document.body.appendChild(meta);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
defaultHomepage(),
|
|
|
|
"hot",
|
|
|
|
"default homepage is pulled from <meta name=discourse_current_homepage>"
|
|
|
|
);
|
2017-11-10 03:45:19 +08:00
|
|
|
document.body.removeChild(meta);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("setDefaultHomepage", assert => {
|
|
|
|
var meta = document.createElement("meta");
|
|
|
|
meta.name = "discourse_current_homepage";
|
|
|
|
meta.content = "hot";
|
|
|
|
document.body.appendChild(meta);
|
|
|
|
setDefaultHomepage("top");
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
meta.content,
|
|
|
|
"top",
|
|
|
|
"default homepage set by setDefaultHomepage"
|
|
|
|
);
|
2017-11-10 03:45:19 +08:00
|
|
|
document.body.removeChild(meta);
|
2013-12-31 01:46:18 +08:00
|
|
|
});
|
2015-12-28 14:28:16 +08:00
|
|
|
|
2017-06-15 01:57:58 +08:00
|
|
|
QUnit.test("caretRowCol", assert => {
|
2018-06-15 23:03:24 +08:00
|
|
|
var textarea = document.createElement("textarea");
|
2015-12-28 14:28:16 +08:00
|
|
|
const content = document.createTextNode("01234\n56789\n012345");
|
|
|
|
textarea.appendChild(content);
|
|
|
|
document.body.appendChild(textarea);
|
|
|
|
|
|
|
|
const assertResult = (setCaretPos, expectedRowNum, expectedColNum) => {
|
2016-06-15 02:31:51 +08:00
|
|
|
setCaretPosition(textarea, setCaretPos);
|
2015-12-28 14:28:16 +08:00
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
const result = caretRowCol(textarea);
|
2018-06-15 23:03:24 +08:00
|
|
|
assert.equal(
|
|
|
|
result.rowNum,
|
|
|
|
expectedRowNum,
|
|
|
|
"returns the right row of the caret"
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
result.colNum,
|
|
|
|
expectedColNum,
|
|
|
|
"returns the right col of the caret"
|
|
|
|
);
|
2015-12-28 14:28:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
assertResult(0, 1, 0);
|
|
|
|
assertResult(5, 1, 5);
|
|
|
|
assertResult(6, 2, 0);
|
|
|
|
assertResult(11, 2, 5);
|
|
|
|
assertResult(14, 3, 2);
|
|
|
|
|
|
|
|
document.body.removeChild(textarea);
|
2017-07-26 03:11:28 +08:00
|
|
|
});
|
2018-01-13 10:43:49 +08:00
|
|
|
|
|
|
|
QUnit.test("fillMissingDates", assert => {
|
|
|
|
const startDate = "2017-11-12"; // YYYY-MM-DD
|
|
|
|
const endDate = "2017-12-12"; // YYYY-MM-DD
|
2018-06-15 23:03:24 +08:00
|
|
|
const data =
|
|
|
|
'[{"x":"2017-11-12","y":3},{"x":"2017-11-27","y":2},{"x":"2017-12-06","y":9},{"x":"2017-12-11","y":2}]';
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
fillMissingDates(JSON.parse(data), startDate, endDate).length,
|
|
|
|
31,
|
|
|
|
"it returns a JSON array with 31 dates"
|
|
|
|
);
|
2018-01-13 10:43:49 +08:00
|
|
|
});
|
2019-12-03 00:48:40 +08:00
|
|
|
|
|
|
|
QUnit.test("getCodeBlocks - works with [code]", assert => {
|
|
|
|
assert.deepEqual(
|
|
|
|
getCodeBlocks("[code]\nfoo\n[/code]\n\nbar\n\n[code]\nbaz"),
|
|
|
|
[
|
|
|
|
[0, 18],
|
|
|
|
[25, 35]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("getCodeBlocks - works with backticks", assert => {
|
|
|
|
assert.deepEqual(getCodeBlocks("foo `bar\nbar`! `baz"), [
|
|
|
|
[4, 13],
|
|
|
|
[15, 19]
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("getCodeBlocks - works with triple backticks", assert => {
|
|
|
|
assert.deepEqual(getCodeBlocks("```\nfoo\n```\n\nbar\n\n```\nbaz"), [
|
|
|
|
[0, 11],
|
|
|
|
[18, 25]
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("inCodeBlock", assert => {
|
|
|
|
const raw =
|
|
|
|
"bar\n\n```\nfoo\n```\n\nbar\n\n`foo\nfoo`\n\nbar\n\n[code]\nfoo\n[/code]\n\nbar`foo";
|
|
|
|
|
|
|
|
assert.notOk(inCodeBlock(raw, 4));
|
|
|
|
assert.ok(inCodeBlock(raw, 5));
|
|
|
|
assert.ok(inCodeBlock(raw, 16));
|
|
|
|
assert.notOk(inCodeBlock(raw, 17));
|
|
|
|
|
|
|
|
assert.notOk(inCodeBlock(raw, 22));
|
|
|
|
assert.ok(inCodeBlock(raw, 23));
|
|
|
|
assert.ok(inCodeBlock(raw, 32));
|
|
|
|
assert.notOk(inCodeBlock(raw, 33));
|
|
|
|
|
|
|
|
assert.notOk(inCodeBlock(raw, 38));
|
|
|
|
assert.ok(inCodeBlock(raw, 39));
|
|
|
|
assert.ok(inCodeBlock(raw, 57));
|
|
|
|
assert.notOk(inCodeBlock(raw, 58));
|
|
|
|
|
|
|
|
assert.notOk(inCodeBlock(raw, 61));
|
|
|
|
assert.ok(inCodeBlock(raw, 62));
|
|
|
|
});
|