Commit adc269ee authored by Luciano Pacheco's avatar Luciano Pacheco Committed by Commit Bot

WebUI: cr.ui.ArrayDataModel unittest type check and nits

Update TestDataSource (chrome://test/) to be able to load chai.js from
//third_party/.

Change chai_assert.js to import chai.js and add assertArrayEquals()
because it used in several tests in WebUI and Files app.

Nits addressed for array_data_model_test.js:
- Add type check.
- Remove "#export" markup.
- Use Object.assign() to export test functions to global scope.

Bug: 1133198
Change-Id: I816fa4d11365ce1adbe769e2cab6993368821e06
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2497960
Auto-Submit: Luciano Pacheco <lucmult@chromium.org>
Reviewed-by: default avatardpapad <dpapad@chromium.org>
Commit-Queue: dpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821159}
parent dcb17ab6
......@@ -104,6 +104,20 @@ void TestDataSource::ReadFile(
GURL url = GetURLForPath(path);
CHECK(url.is_valid());
if (url.path() == "/chai.js") {
base::FilePath src_root;
CHECK(base::PathService::Get(base::DIR_SOURCE_ROOT, &src_root));
base::FilePath file_path =
src_root.AppendASCII("third_party/chaijs/chai.js")
.NormalizePathSeparators();
CHECK(base::ReadFileToString(file_path, &content))
<< url.spec() << "=" << file_path.value();
scoped_refptr<base::RefCountedString> response =
base::RefCountedString::TakeString(&content);
std::move(callback).Run(response.get());
return;
}
if (base::StartsWith(url.query(), kModuleQuery,
base::CompareCase::INSENSITIVE_ASCII)) {
std::string js_path = url.query().substr(strlen(kModuleQuery));
......
......@@ -484,6 +484,7 @@ group("closure_compile") {
"cr_components:closure_compile",
"cr_elements:closure_compile",
"inline_login:closure_compile",
"js/cr/ui:closure_compile",
"new_tab_page:closure_compile",
"print_preview:closure_compile",
"read_later:closure_compile",
......
......@@ -4,6 +4,8 @@
/** @fileoverview Assertion helper functions wrapping the chaijs API. */
import './chai.js';
/**
* @param {boolean} value The value to check.
* @param {string=} opt_message Additional error message.
......@@ -128,3 +130,12 @@ export function assertThrows(
testFunction,
/** @type{string} */ (opt_expected_or_constructor), opt_message);
}
/**
* Verifies that the contents of the expected and observed arrays match.
* @param {!Array} expected The expected result.
* @param {!Array} actual The actual result.
*/
export function assertArrayEquals(expected, actual) {
assertDeepEquals(expected, actual);
}
......@@ -2,6 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//third_party/closure_compiler/compile_js.gni")
import("//ui/webui/resources/js/cr.gni")
import("//ui/webui/resources/tools/js_modulizer.gni")
......@@ -23,3 +24,20 @@ js_modulizer("modulize") {
namespace_rewrites = cr_namespace_rewrites
}
js_library("array_data_model_test.m") {
sources = [
"$root_gen_dir/chrome/test/data/webui/js/cr/ui/array_data_model_test.m.js",
]
deps = [ "//ui/webui/resources/js/cr/ui:array_data_model.m" ]
extra_deps = [ ":modulize" ]
}
js_type_check("closure_compile") {
uses_js_modules = true
deps = [
":array_data_model_test.m",
"../../..:chai_assert",
]
}
......@@ -4,16 +4,17 @@
// clang-format off
// #import {ArrayDataModel} from 'chrome://resources/js/cr/ui/array_data_model.m.js';
// #import {assertArrayEquals, assertEquals} from '../../../chai_assert.js';
// clang-format on
/* #export */ function testSlice() {
function testSlice() {
var m = new cr.ui.ArrayDataModel([0, 1, 2]);
assertArrayEquals([0, 1, 2], m.slice());
assertArrayEquals([1, 2], m.slice(1));
assertArrayEquals([1], m.slice(1, 2));
}
/* #export */ function testPush() {
function testPush() {
var m = new cr.ui.ArrayDataModel([0, 1, 2]);
var count = 0;
......@@ -31,7 +32,7 @@
assertEquals(1, count, 'The splice event should only fire once');
}
/* #export */ function testSplice() {
function testSplice() {
function compare(array, args) {
var m = new cr.ui.ArrayDataModel(array.slice());
var expected = array.slice();
......@@ -50,7 +51,7 @@
compare([1, 2, 3], [5, 3, 1, 2, 3]);
}
/* #export */ function testPermutation() {
function testPermutation() {
function doTest(sourceArray, spliceArgs) {
var m = new cr.ui.ArrayDataModel(sourceArray.slice());
var permutation;
......@@ -77,7 +78,7 @@
doTest([1, 2, 3], [0, 3, 1, 2, 3]);
}
/* #export */ function testUpdateIndexes() {
function testUpdateIndexes() {
var m = new cr.ui.ArrayDataModel([1, 2, 3]);
var changedIndexes = [];
m.addEventListener('change', function(event) {
......@@ -87,7 +88,7 @@
assertArrayEquals([0, 1, 2], changedIndexes);
}
/* #export */ function testReplaceItem() {
function testReplaceItem() {
var m = new cr.ui.ArrayDataModel([1, 2, 3]);
var permutation = null;
var changeIndex;
......@@ -102,9 +103,11 @@
assertEquals(1, changeIndex);
}
window.testSlice = testSlice;
window.testPush = testPush;
window.testSplice = testSplice;
window.testPermutation = testPermutation;
window.testUpdateIndexes = testUpdateIndexes;
window.testReplaceItem = testReplaceItem;
Object.assign(window, {
testSlice,
testPush,
testSplice,
testPermutation,
testUpdateIndexes,
testReplaceItem,
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment