Skip to content

Commit

Permalink
feat: Add Unit Test (#8)
Browse files Browse the repository at this point in the history
* feat: Add Unit Test

* fix: typo target add rustup cmd

* fix: missing install wasm-pack in ci

* fix: Missing manifest.json
  • Loading branch information
afifurrohman-id authored May 30, 2024
1 parent de74eb7 commit 3e4784d
Show file tree
Hide file tree
Showing 12 changed files with 427 additions and 178 deletions.
47 changes: 41 additions & 6 deletions .github/workflows/live.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,55 @@ on:
branches: main

jobs:
check:
runs-on: ubuntu-latest
strategy:
matrix:
profile: [dev,release]
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2

- run: rustup target add wasm32-unknown-unknown

- name: Check
run: |
cargo clippy --profile ${{matrix.profile}} && \
cargo fmt --check
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2

- run: rustup target add wasm32-unknown-unknown

- run: cargo install wasm-pack

- name: Test (unit)
run: make test -j 8

spell-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: crate-ci/typos@v1.20.1

deploy:
runs-on: ubuntu-latest
needs:
- check
- test
- spell-check
permissions:
pull-requests: write
checks: write
steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
targets: wasm32-unknown-unknown
components: clippy, rustfmt

- run: rustup target add wasm32-unknown-unknown

- run: cargo install wasm-pack

- run: make release -j 8
Expand Down
45 changes: 40 additions & 5 deletions .github/workflows/preview.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,54 @@ on:
branches: main

jobs:
check:
runs-on: ubuntu-latest
strategy:
matrix:
profile: [dev,release]
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2

- run: rustup target add wasm32-unknown-unknown

- name: Check
run: |
cargo clippy --profile ${{matrix.profile}} && \
cargo fmt --check
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2

- run: rustup target add wasm32-unknown-unknown

- run: cargo install wasm-pack

- name: Test (unit)
run: make test -j 8

spell-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: crate-ci/typos@v1.20.1

preview:
runs-on: ubuntu-latest
needs:
- check
- test
- spell-check
permissions:
pull-requests: write
checks: write
steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
targets: wasm32-unknown-unknown
components: clippy, rustfmt
- run: rustup target add wasm32-unknown-unknown

- run: cargo install wasm-pack

Expand Down
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
image = "0.25"
console_error_panic_hook = "0.1"
wasm-bindgen-test = "0.3"

[dependencies.web-sys]
version = "0.3"
Expand All @@ -33,6 +34,7 @@ features = [
"Blob",
"BlobPropertyBag",
"Window",
"console"
]

[profile.release]
Expand Down
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ WASM_FLAGS=--no-typescript \
--no-pack -t web \
-d assets
build: src/lib.rs
$(MAKE) fix
cargo fmt
wasm-pack build --dev $(WASM_FLAGS)
release: src/lib.rs
$(MAKE) fix
cargo fmt
wasm-pack build $(WASM_FLAGS)

fix:
cargo clippy --fix --allow-dirty --allow-staged
cargo clippy --fix --allow-dirty --allow-staged -r
test: src/lib.rs
wasm-pack test --node

server: compose.yaml
docker compose up -d
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- [x] Wasm Pack Cli (version >= 0.12.x)
- [x] Git (version >= 2.43.x)
- [x] Docker (version >= 24.0.x)
- [ ] Node (version >= 20.14.x)
> Only if you want to test

### Installation
Expand Down Expand Up @@ -54,3 +56,8 @@ make
```sh
make release
```

- Test (Unit Test)
```sh
make test
```
19 changes: 19 additions & 0 deletions assets/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "Enku - Crack puzzle get the word",
"short_name": "Enku",
"description": "Belajar bahasa inggris melalui game puzzle",
"id": "/",
"scope": "/",
"start_url": "/",
"background_color": "#ffffff",
"theme_color": "#000000",
"display": "standalone",
"icons": [
],
"screenshots": [
],
"display_override": [
"standalone",
"window-controls-overlay"
]
}
26 changes: 26 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,29 @@ pub fn bytes_to_js_blob(buffer: &[u8], mime: &str) -> Blob {

Blob::new_with_u8_array_sequence_and_options(&arr, &opts).unwrap()
}

#[cfg(test)]
mod tests {
use wasm_bindgen_test::wasm_bindgen_test;
use web_sys::js_sys::Uint8Array;

#[wasm_bindgen_test]
fn bytes_to_js_blob() {
let mime = "text/plain";
let blob = super::bytes_to_js_blob(&[0b01100001, 0b01100001, 0b01100001], mime);

assert_eq!(blob.type_(), mime);
assert_eq!(blob.size(), 3.0);
}

#[wasm_bindgen_test]
fn js_buffer_to_bytes() {
let arr = &[1, 2, 3];
let js_bytes = Uint8Array::new_with_length(arr.len() as u32);
js_bytes.copy_from(arr);

let bytes = super::js_buffer_to_bytes(&js_bytes);
assert_eq!(bytes.len(), arr.len());
assert_eq!(bytes, arr);
}
}
Loading

0 comments on commit 3e4784d

Please sign in to comment.