Skip to content

Commit 1371f21

Browse files
committed
chore: Add Rust & TypeScript tests for WASM crate
1 parent b55b81a commit 1371f21

File tree

7 files changed

+141
-1
lines changed

7 files changed

+141
-1
lines changed

.github/workflows/build.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,51 @@ jobs:
136136
- name: Run ${{ matrix.python }} tox job
137137
run: tox -e py
138138
working-directory: ./python
139+
140+
test-wasm:
141+
name: Tests for WASM crate
142+
runs-on: ubuntu-latest
143+
steps:
144+
- uses: actions/checkout@v2
145+
with:
146+
submodules: true
147+
- uses: actions-rs/toolchain@v1
148+
with:
149+
profile: minimal
150+
toolchain: stable
151+
override: true
152+
- name: Install wasm-pack
153+
uses: actions-rs/cargo@v1
154+
with:
155+
command: install
156+
args: wasm-pack
157+
- name: Run tests
158+
run: wasm-pack test --node
159+
working-directory: ./wasm
160+
161+
test-wasm-typescript:
162+
name: TypeScript tests for WASM crate
163+
runs-on: ubuntu-latest
164+
steps:
165+
- uses: actions/checkout@v2
166+
with:
167+
submodules: true
168+
- uses: actions-rs/toolchain@v1
169+
with:
170+
profile: minimal
171+
toolchain: stable
172+
override: true
173+
- name: Install wasm-pack
174+
uses: actions-rs/cargo@v1
175+
with:
176+
command: install
177+
args: wasm-pack
178+
- name: Build package
179+
run: wasm-pack build -t nodejs
180+
working-directory: ./wasm
181+
- name: Install dependencies
182+
run: npm install
183+
working-directory: ./wasm
184+
- name: Run tests
185+
run: npm run test
186+
working-directory: ./wasm

wasm/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# Rust
22
/target
3+
node_modules
4+
package-lock.json

wasm/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ serde = { version = "1.0", features = ["derive"] }
2222
serde_derive = "1"
2323
serde_json = "1"
2424

25+
[dev-dependencies]
26+
wasm-bindgen-test = "0.3.0"
27+
2528
[profile.release]
2629
codegen-units = 1
2730
lto = "fat"

wasm/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "wasm",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"dependencies": {
7+
"tslib": "^2.0.0"
8+
},
9+
"devDependencies": {
10+
"@types/chai": "^4.2.11",
11+
"@types/mocha": "^8.0.0",
12+
"chai": "^4.2.0",
13+
"mocha": "^8.0.1",
14+
"prettier": "2.0.5",
15+
"ts-node": "^8.10.2",
16+
"typescript": "^3.9.6"
17+
},
18+
"scripts": {
19+
"test": "mocha --require ts-node/register tests-ts/*.ts"
20+
},
21+
"keywords": [],
22+
"author": "Dmitry Dygalo",
23+
"license": "ISC"
24+
}

wasm/src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn parse_url(url: Option<String>) -> Result<Option<url::Url>, JsValue> {
5858
#[macro_use]
5959
extern crate serde_derive;
6060

61-
#[derive(Deserialize)]
61+
#[derive(Serialize, Deserialize)]
6262
#[serde(default)]
6363
struct Options {
6464
inline_style_tags: bool,
@@ -126,3 +126,26 @@ interface InlineOptions {
126126
127127
export function inline(html: string, options?: InlineOptions): string;
128128
"#;
129+
130+
#[cfg(test)]
131+
pub mod tests {
132+
use super::*;
133+
use wasm_bindgen_test::*;
134+
135+
#[wasm_bindgen_test]
136+
fn default_config() {
137+
let result = inline("<html><head><title>Test</title><style>h1 { color:red; }</style></head><body><h1>Test</h1></body></html>", &JsValue::undefined()).unwrap();
138+
assert_eq!(result, "<html><head><title>Test</title><style>h1 { color:red; }</style></head><body><h1 style=\"color:red;\">Test</h1></body></html>");
139+
}
140+
141+
#[wasm_bindgen_test]
142+
fn remove_style_tags() {
143+
let options = Options {
144+
remove_style_tags: true,
145+
..Default::default()
146+
};
147+
let options = JsValue::from_serde(&options).unwrap();
148+
let result = inline("<html><head><title>Test</title><style>h1 { color:red; }</style></head><body><h1>Test</h1></body></html>", &options).unwrap();
149+
assert_eq!(result, "<html><head><title>Test</title></head><body><h1 style=\"color:red;\">Test</h1></body></html>");
150+
}
151+
}

wasm/tests-ts/css_inline.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expect } from "chai";
2+
import { inline } from "../pkg/css_inline";
3+
4+
describe("CSS inliner", () => {
5+
describe("default inlining", () => {
6+
it("h1 style should be applied", function () {
7+
expect(
8+
inline(
9+
"<html><head><title>Test</title><style>h1 { color:red; }</style></head><body><h1>Test</h1></body></html>"
10+
)
11+
).to.equal(
12+
'<html><head><title>Test</title><style>h1 { color:red; }</style></head><body><h1 style="color:red;">Test</h1></body></html>'
13+
);
14+
});
15+
it("style tag is removed", function () {
16+
expect(
17+
inline(
18+
"<html><head><title>Test</title><style>h1 { color:red; }</style></head><body><h1>Test</h1></body></html>",
19+
{ remove_style_tags: true }
20+
)
21+
).to.equal(
22+
'<html><head><title>Test</title></head><body><h1 style="color:red;">Test</h1></body></html>'
23+
);
24+
});
25+
});
26+
});

wasm/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./dist/",
4+
"allowJs": true,
5+
"skipLibCheck": true,
6+
"target": "es5",
7+
"moduleResolution": "node",
8+
"module": "commonjs",
9+
"strict": true,
10+
"importHelpers": true,
11+
"lib": ["dom", "es5", "es2015.promise"]
12+
},
13+
"exclude": ["node_modules", "pkg"]
14+
}

0 commit comments

Comments
 (0)