Skip to content

Commit 226aa48

Browse files
authored
Add test for Knockout semanticui data binding (#215)
* Switch to web-test-runner Reusing some of what I went through with the addons testing work, I tried to use web-test-runner here as well. Even though we aren't using web components here yet, this pattern still works quite nicely for testing some complex browser integration style tests. This uses WTR, Rollup for bundling for tests, and I've created two test cases: I translated old tests to Chai/WTR, and a new test that mixes HTML and JS, using WTR HTML tests. * Remove unneeded hack * Update CI config * More CI config * Some cleanup and formatting * Add test for Knockout semanticui data binding This confirms that the custom binding for SUI modules works as expected by inspecting the element and confirming it seems to have had our logic and the jQuery SUI plugin logic applied to it. This found a small bug with the binding code itself. I only tested dropdown for now, I'm not sure if it's worth testing anything too deeply yet.
1 parent ba78f7d commit 226aa48

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

readthedocsext/theme/static/readthedocsext/theme/js/site.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/application/index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import * as project_views from "../project";
2222
* :meth:`Application.run`.
2323
*/
2424
export class Application {
25-
constructor() {
25+
constructor(config) {
26+
this.config = config;
2627
this.registry = new Registry();
2728
}
2829

@@ -50,21 +51,22 @@ export class Application {
5051
* Convention on :ref:`js-json-config`
5152
*/
5253
load_config() {
53-
console.debug("Loading site front end configuration from script tag");
54-
55-
const site_config_src = jquery("script#site-config").text() || "{}";
56-
const site_config = JSON.parse(site_config_src);
57-
if (site_config.webpack_public_path) {
58-
__webpack_public_path__ = site_config.webpack_public_path;
54+
if (this.config === undefined) {
55+
console.debug("Loading site front end configuration from script tag");
56+
const site_config_src = jquery("script#site-config").text() || "{}";
57+
this.config = JSON.parse(site_config_src);
58+
}
59+
if (this.config.webpack_public_path) {
60+
__webpack_public_path__ = this.config.webpack_public_path;
5961
global.__webpack_public_path__ = window.__webpack_public_path__ =
60-
site_config.webpack_public_path;
62+
this.config.webpack_public_path;
6163
}
6264
// Null route debug logging, don't do output anything that was debug
63-
if (!site_config.debug) {
65+
if (!this.config.debug) {
6466
console.debug = () => {};
6567
}
6668

67-
return site_config;
69+
return this.config;
6870
}
6971

7072
/**

src/js/application/plugins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ export const message = {
347347
*/
348348
export const semanticui = {
349349
update: (element, value_accessor, all_bindings) => {
350-
const value = ko.unwrap(value_accessor());
350+
const binding_value = ko.unwrap(value_accessor());
351351
const jq_element = jquery(element);
352-
for (const [key, value] of Object.entries(value)) {
352+
for (const [key, value] of Object.entries(binding_value)) {
353353
if (key === "modal") {
354354
// modal is not supported here because the jQuery ``modal()`` plugin
355355
// replaces ``<body>`` and this causes an error from Knockout, because
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<html>
2+
<body>
3+
<div class="ui dropdown" data-bind="semanticui: { dropdown: {}}">
4+
Dropdown
5+
<i class="dropdown icon"></i>
6+
<div class="menu">
7+
<div class="item">Choice 1</div>
8+
<div class="item">Choice 2</div>
9+
</div>
10+
</div>
11+
12+
<script type="module">
13+
import { expect } from "@open-wc/testing";
14+
import { runTests } from "@web/test-runner-mocha";
15+
import { default as ko } from "knockout";
16+
import { default as jquery } from "jquery";
17+
18+
import { Application } from "../../application";
19+
import { Registry } from "../../application/registry";
20+
21+
const app = new Application({ debug: true });
22+
app.run();
23+
24+
runTests(async () => {
25+
describe("Knockout semanticui plugin", () => {
26+
it("SUI module initialized successfully", () => {
27+
const elem = jquery(".ui.dropdown");
28+
29+
// The HTML attribute we add. Note `data()` is a jQuery method that
30+
// can convert HTML data attributes to camelCase attribute names.
31+
const data = elem.data();
32+
expect(data.semanticuiDropdown).to.be.true;
33+
34+
// Inspect internal attributes added to the element by the FUI
35+
// module plugin. For example, the module is attached to the element
36+
// here:
37+
// https://github.com/fomantic/Fomantic-UI/blob/b19851433cef2feba58b036cc19dcc72da176f1b/src/definitions/modules/dropdown.js#L143C26-L149
38+
const moduleData = elem.data("module-dropdown");
39+
expect(moduleData.can.show()).to.be.true;
40+
expect(moduleData.is.active()).to.be.false;
41+
expect(moduleData.is.disabled()).to.be.false;
42+
expect(moduleData.is.empty()).to.be.false;
43+
});
44+
});
45+
});
46+
</script>
47+
</body>
48+
</html>

0 commit comments

Comments
 (0)