Skip to content

Commit

Permalink
Merge pull request #18 from hamadr/feat-module-es6
Browse files Browse the repository at this point in the history
feat: javascript is now inside a ES6 module
  • Loading branch information
DylannCordel authored Oct 17, 2023
2 parents 980bcc6 + 0b9c960 commit 2eb70f7
Show file tree
Hide file tree
Showing 9 changed files with 1,544 additions and 242 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ url_patterns = [

## Usage

Add `dalec/js/main.js` inside templates where you need to use the dalec.

Each dalec's child app will probably need some specific configuration to retrieve external contents
(eg: token or login/password). Please refer to this dalec's child app configuration section first.

Expand Down
4 changes: 1 addition & 3 deletions dalec/static/dalec/js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function dalec_fetch_content(container) {
export function fetch_content(container) {
const [orderedBy, url] = [container.dataset.orderedBy, container.dataset.url];
let channelObjects = container.dataset.channelObjects;

Expand Down Expand Up @@ -36,5 +36,3 @@ function dalec_fetch_content(container) {
});
});
}

module.exports = dalec_fetch_content;
9 changes: 5 additions & 4 deletions dalec/templates/dalec/default/list.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% load static %}
{% block dalec_list_header %}
{% endblock dalec_list_header %}

Expand Down Expand Up @@ -27,10 +28,10 @@
</div>

{% if ajax_refresh %}
<script type="text/javascript">
window.addEventListener("load", (event) => {
dalecElement = document.getElementById('dalec-{{ id }}');
dalec_fetch_content(dalecElement);
<script type="module">
import("{% static 'dalec/js/main.js' %}").then((DalecModule) => {
const dalecElement = document.getElementById('dalec-{{ id }}');
DalecModule.fetch_content(dalecElement);
});
</script>
{% endif %}
Expand Down
2 changes: 2 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
"NAME": os.path.join(BASE_PATH, "tests.sqlite3"),
}
}

STATIC_URL = "/static/"
6 changes: 6 additions & 0 deletions tests_js/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
1 change: 0 additions & 1 deletion tests_js/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "jsdom",
};
14 changes: 7 additions & 7 deletions tests_js/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const dalec_fetch_content = require("../dalec/static/dalec/js/main.js");
import { fetch_content } from "../dalec/static/dalec/js/main.js";

let dalecContainer: HTMLElement;

describe("dalec_fetch_content", () => {
describe("fetch_content", () => {
describe("without channelObjects nor orderBy", () => {
beforeAll(() => {
document.body.innerHTML = `
Expand All @@ -16,7 +16,7 @@ describe("dalec_fetch_content", () => {
});
}) as jest.Mock;
dalecContainer = document.getElementById("dalec-1");
dalec_fetch_content(dalecContainer);
fetch_content(dalecContainer);
});

it("should fetch url", () => {
Expand Down Expand Up @@ -52,7 +52,7 @@ describe("dalec_fetch_content", () => {
}),
) as jest.Mock;
dalecContainer = document.getElementById("dalec-1");
dalec_fetch_content(dalecContainer);
fetch_content(dalecContainer);
});

it("should call fetch with channelObjects and orderedBy", () => {
Expand All @@ -76,7 +76,7 @@ describe("dalec_fetch_content", () => {
}),
) as jest.Mock;
dalecContainer = document.getElementById("dalec-1");
dalec_fetch_content(dalecContainer);
fetch_content(dalecContainer);
});

it("should call fetch with channelObjects", () => {
Expand All @@ -101,7 +101,7 @@ describe("dalec_fetch_content", () => {
}),
) as jest.Mock;
dalecContainer = document.getElementById("dalec-1");
dalec_fetch_content(dalecContainer);
fetch_content(dalecContainer);
});

it("should call fetch with orderedBy", () => {
Expand All @@ -125,7 +125,7 @@ describe("dalec_fetch_content", () => {
}),
) as jest.Mock;
dalecContainer = document.getElementById("dalec-1");
dalec_fetch_content(dalecContainer);
fetch_content(dalecContainer);
});

it("should not have the loading css class", () => {
Expand Down
Loading

0 comments on commit 2eb70f7

Please sign in to comment.