Skip to content

Commit 0deabd1

Browse files
committed
build: add coverage report
1 parent edd180b commit 0deabd1

8 files changed

+111
-12
lines changed

.github/workflows/build.yml

+11-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ jobs:
2424
- name: Install dependencies
2525
run: npm install
2626

27-
- name: Build Angular project
28-
run: npm run build -- --prod
27+
- name: Run CI build
28+
run: npm run ci:build
29+
30+
- name: Run CI tests
31+
run: npm run ci:test
32+
33+
- name: Upload coverage report
34+
uses: actions/upload-artifact@v3
35+
with:
36+
name: coverage-report
37+
path: coverage
2938

3039
- name: Publish Package
3140
uses: JS-DevTools/npm-publish@v3

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-web-serial",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Angular library for Web Serial API with RxJS",
55
"repository": {
66
"type": "git",
@@ -17,7 +17,9 @@
1717
"start": "ng serve",
1818
"build": "ng build",
1919
"watch": "ng build --watch --configuration development",
20-
"test": "ng test"
20+
"test": "ng test",
21+
"ci:build": "ng build --prod",
22+
"ci:test": "ng test --watch=false --browsers ChromeHeadless --code-coverage"
2123
},
2224
"private": false,
2325
"dependencies": {

projects/angular-serial/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-web-serial",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"peerDependencies": {
55
"@angular/common": "^17.3.0",
66
"@angular/core": "^17.3.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule, DOCUMENT } from '@angular/common';
3+
import { AngularSerialService } from './angular-serial.service';
4+
import { MockSerial } from './mock-serial';
5+
6+
7+
8+
@NgModule({
9+
declarations: [],
10+
imports: [
11+
CommonModule
12+
],
13+
providers: [
14+
AngularSerialService,
15+
{
16+
provide: 'Serial',
17+
useClass: MockSerial
18+
}
19+
]
20+
})
21+
export class AngularSerialTestingModule { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule, DOCUMENT } from '@angular/common';
3+
import { AngularSerialService } from './angular-serial.service';
4+
5+
@NgModule({
6+
imports: [
7+
CommonModule
8+
],
9+
providers: [
10+
AngularSerialService,
11+
{
12+
provide: 'Serial',
13+
useFactory: (document: Document) => document.defaultView?.navigator?.serial,
14+
deps: [DOCUMENT]
15+
}
16+
]
17+
})
18+
export class AngularSerialModule { }
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
11
import { TestBed } from '@angular/core/testing';
22

33
import { AngularSerialService } from './angular-serial.service';
4+
import { AngularSerialTestingModule } from './angular-serial-testing.module';
5+
import { switchMap } from 'rxjs';
46

57
describe('AngularSerialService', () => {
68
let service: AngularSerialService;
79

810
beforeEach(() => {
9-
TestBed.configureTestingModule({});
11+
TestBed.configureTestingModule({
12+
imports: [AngularSerialTestingModule]
13+
});
1014
service = TestBed.inject(AngularSerialService);
1115
});
1216

1317
it('should be created', () => {
1418
expect(service).toBeTruthy();
1519
});
20+
21+
it('should flag service as opened', (done) => {
22+
service.open().subscribe(() => {
23+
service.isConnected().subscribe((connected) => {
24+
expect(connected).toBeTrue();
25+
done();
26+
});
27+
});
28+
});
29+
30+
31+
it('should echo back write value on read', (done) => {
32+
service.open().pipe(
33+
switchMap(() => service.write('test')),
34+
switchMap(() => service.read())
35+
).subscribe((value) => {
36+
expect(value).toBe('test');
37+
service.close();
38+
done();
39+
});
40+
});
41+
42+
43+
44+
1645
});

projects/angular-serial/src/lib/angular-serial.service.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ import {
1212
} from 'rxjs';
1313
import { DOCUMENT } from '@angular/common';
1414

15-
@Injectable({
16-
providedIn: 'root'
17-
})
15+
@Injectable()
1816
export class AngularSerialService {
1917

20-
readonly serial: Serial | undefined;
2118
private port: SerialPort | null = null;
2219
private abortController: AbortController | null = null;
2320
private dataStream: WritableStream | null = null;
@@ -34,10 +31,9 @@ export class AngularSerialService {
3431
}
3532

3633
constructor(
37-
@Inject(DOCUMENT) private document: Document,
34+
@Inject('Serial') readonly serial: Serial | undefined,
3835
private ngZone: NgZone
3936
) {
40-
this.serial = this.document?.defaultView?.navigator?.serial;
4137
}
4238

4339
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export class MockSerial {
2+
private readableController: ReadableStreamDefaultController<Uint8Array> | null = null;
3+
4+
requestPort(options?: SerialPortRequestOptions): Promise<SerialPort> {
5+
return Promise.resolve({
6+
open: () => Promise.resolve(),
7+
close: () => Promise.resolve(),
8+
readable: new ReadableStream({
9+
start: (controller) => {
10+
this.readableController = controller;
11+
}
12+
}),
13+
writable: new WritableStream({
14+
write: (chunk) => {
15+
const input = new TextDecoder().decode(chunk);
16+
let response: string = input.trim();
17+
if (this.readableController) {
18+
this.readableController.enqueue(new TextEncoder().encode(response));
19+
}
20+
}
21+
})
22+
} as any as SerialPort);
23+
}
24+
}

0 commit comments

Comments
 (0)