discourse/test/javascripts/ember/resolver-test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

264 lines
6.3 KiB
JavaScript
Raw Normal View History

import { setResolverOption, buildResolver } from "discourse-common/resolver";
2013-09-25 12:29:15 -04:00
2016-02-18 11:53:25 -05:00
let originalTemplates;
let resolver;
2013-09-25 12:29:15 -04:00
2017-06-14 13:57:58 -04:00
function lookupTemplate(assert, name, expectedTemplate, message) {
2014-12-11 15:28:49 -05:00
var parseName = resolver.parseName(name);
var result = resolver.resolveTemplate(parseName);
2017-06-14 13:57:58 -04:00
assert.equal(result, expectedTemplate, message);
}
2014-08-07 11:47:45 -04:00
function setTemplates(lookupTemplateStrings) {
lookupTemplateStrings.forEach(function(lookupTemplateString) {
Ember.TEMPLATES[lookupTemplateString] = lookupTemplateString;
2013-09-25 12:29:15 -04:00
});
}
2013-09-25 12:29:15 -04:00
const DiscourseResolver = buildResolver("discourse");
2017-06-14 13:57:58 -04:00
QUnit.module("lib:resolver", {
beforeEach() {
2013-09-25 12:29:15 -04:00
originalTemplates = Ember.TEMPLATES;
Ember.TEMPLATES = {};
2016-02-18 11:53:25 -05:00
resolver = DiscourseResolver.create();
2013-09-25 12:29:15 -04:00
},
2017-06-14 13:57:58 -04:00
afterEach() {
2013-09-25 12:29:15 -04:00
Ember.TEMPLATES = originalTemplates;
}
});
2017-06-14 13:57:58 -04:00
QUnit.test("finds templates in top level dir", assert => {
2013-09-25 12:29:15 -04:00
setTemplates(["foobar", "fooBar", "foo_bar", "foo.bar"]);
2017-06-14 13:57:58 -04:00
lookupTemplate(assert, "template:foobar", "foobar", "by lowcased name");
lookupTemplate(assert, "template:fooBar", "fooBar", "by camel cased name");
lookupTemplate(assert, "template:foo_bar", "foo_bar", "by underscored name");
lookupTemplate(assert, "template:foo.bar", "foo.bar", "by dotted name");
2013-09-25 12:29:15 -04:00
});
2017-06-14 13:57:58 -04:00
QUnit.test("finds templates in first-level subdir", assert => {
setTemplates(["foo/bar_baz"]);
2013-09-25 12:29:15 -04:00
2017-06-14 13:57:58 -04:00
lookupTemplate(
assert,
"template:foo/bar_baz",
"foo/bar_baz",
"with subdir defined by slash"
2018-06-15 11:03:24 -04:00
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:foo.bar_baz",
"foo/bar_baz",
"with subdir defined by dot"
2018-06-15 11:03:24 -04:00
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:fooBarBaz",
"foo/bar_baz",
"with subdir defined by first camel case and the rest of camel cases converted to underscores"
);
lookupTemplate(
assert,
"template:foo_bar_baz",
"foo/bar_baz",
"with subdir defined by first underscore"
);
2013-09-25 12:29:15 -04:00
});
2017-06-14 13:57:58 -04:00
QUnit.test(
"resolves precedence between overlapping top level dir and first level subdir templates",
assert => {
setTemplates(["fooBar", "foo_bar", "foo.bar", "foo/bar"]);
2018-06-15 11:03:24 -04:00
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:foo.bar",
"foo/bar",
"preferring first level subdir for dotted name"
2018-06-15 11:03:24 -04:00
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:fooBar",
"fooBar",
"preferring top level dir for camel cased name"
2018-06-15 11:03:24 -04:00
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:foo_bar",
"foo_bar",
"preferring top level dir for underscored name"
2018-06-15 11:03:24 -04:00
);
}
);
2017-06-14 13:57:58 -04:00
QUnit.test("finds templates in subdir deeper than one level", assert => {
setTemplates(["foo/bar/baz/qux"]);
2018-06-15 11:03:24 -04:00
2017-06-14 13:57:58 -04:00
lookupTemplate(
assert,
"template:foo/bar/baz/qux",
"foo/bar/baz/qux",
"for subdirs defined by slashes"
);
lookupTemplate(
assert,
"template:foo.bar.baz.qux",
"foo/bar/baz/qux",
"for subdirs defined by dots"
);
lookupTemplate(
assert,
"template:foo/bar/bazQux",
"foo/bar/baz/qux",
"for subdirs defined by slashes plus one camel case"
);
lookupTemplate(
assert,
"template:foo/bar/baz_qux",
"foo/bar/baz/qux",
"for subdirs defined by slashes plus one underscore"
);
2018-06-15 11:03:24 -04:00
2017-06-14 13:57:58 -04:00
lookupTemplate(
assert,
"template:fooBarBazQux",
undefined,
"but not for subdirs defined by more than one camel case"
);
lookupTemplate(
assert,
"template:foo_bar_baz_qux",
undefined,
"but not for subdirs defined by more than one underscore"
);
lookupTemplate(
assert,
"template:foo.bar.bazQux",
undefined,
"but not for subdirs defined by dots plus one camel case"
);
lookupTemplate(
assert,
"template:foo.bar.baz_qux",
undefined,
"but not for subdirs defined by dots plus one underscore"
2018-06-15 11:03:24 -04:00
);
2013-09-25 12:29:15 -04:00
});
2017-06-14 13:57:58 -04:00
QUnit.test("resolves mobile templates to 'mobile/' namespace", assert => {
2013-09-25 12:29:15 -04:00
setTemplates(["mobile/foo", "bar", "mobile/bar", "baz"]);
setResolverOption("mobileView", true);
2013-09-25 12:29:15 -04:00
2017-06-14 13:57:58 -04:00
lookupTemplate(
assert,
"template:foo",
"mobile/foo",
"finding mobile version even if normal one is not present"
);
lookupTemplate(
assert,
"template:bar",
"mobile/bar",
"preferring mobile version when both mobile and normal versions are present"
);
lookupTemplate(
assert,
"template:baz",
"baz",
"falling back to a normal version when mobile version is not present"
);
2013-09-25 12:29:15 -04:00
});
2017-06-14 13:57:58 -04:00
QUnit.test("resolves plugin templates to 'javascripts/' namespace", assert => {
setTemplates(["javascripts/foo", "bar", "javascripts/bar", "baz"]);
2018-06-15 11:03:24 -04:00
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:foo",
"javascripts/foo",
2017-06-14 13:57:58 -04:00
"finding plugin version even if normal one is not present"
2018-06-15 11:03:24 -04:00
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
"template:bar",
"javascripts/bar",
2017-06-14 13:57:58 -04:00
"preferring plugin version when both versions are present"
);
lookupTemplate(
assert,
"template:baz",
"baz",
"falling back to a normal version when plugin version is not present"
);
});
2017-06-14 13:57:58 -04:00
QUnit.test(
"resolves templates with 'admin' prefix to 'admin/templates/' namespace",
assert => {
2013-09-25 12:29:15 -04:00
setTemplates([
"admin/templates/foo",
"adminBar",
"admin_bar",
"admin.bar",
"admin/templates/bar"
]);
2017-06-14 13:57:58 -04:00
lookupTemplate(
assert,
"template:adminFoo",
"admin/templates/foo",
"when prefix is separated by camel case"
2018-06-15 11:03:24 -04:00
);
2013-09-25 12:29:15 -04:00
lookupTemplate(
assert,
"template:admin_foo",
2017-06-14 13:57:58 -04:00
"admin/templates/foo",
2013-09-25 12:29:15 -04:00
"when prefix is separated by underscore"
2018-06-15 11:03:24 -04:00
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:admin.foo",
"admin/templates/foo",
2013-09-25 12:29:15 -04:00
"when prefix is separated by dot"
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:adminfoo",
undefined,
"but not when prefix is not separated in any way"
2018-06-15 11:03:24 -04:00
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:adminBar",
"adminBar",
"but not when template with the exact camel cased name exists"
2018-06-15 11:03:24 -04:00
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:admin_bar",
"admin_bar",
"but not when template with the exact underscored name exists"
2018-06-15 11:03:24 -04:00
);
2017-06-14 13:57:58 -04:00
lookupTemplate(
2018-06-15 11:03:24 -04:00
assert,
2017-06-14 13:57:58 -04:00
"template:admin.bar",
"admin.bar",
"but not when template with the exact dotted name exists"
2018-06-15 11:03:24 -04:00
);
}
);
2017-06-14 13:57:58 -04:00
QUnit.test(
"returns 'not_found' template when template name cannot be resolved",
assert => {
setTemplates(["not_found"]);
2018-06-15 11:03:24 -04:00
2017-06-14 13:57:58 -04:00
lookupTemplate(assert, "template:foo/bar/baz", "not_found", "");
2013-09-25 12:29:15 -04:00
}
);