File tree 8 files changed +111
-12
lines changed
8 files changed +111
-12
lines changed Original file line number Diff line number Diff line change 24
24
- name : Install dependencies
25
25
run : npm install
26
26
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
29
38
30
39
- name : Publish Package
31
40
uses : JS-DevTools/npm-publish@v3
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " angular-web-serial" ,
3
- "version" : " 0.0.5 " ,
3
+ "version" : " 0.0.6 " ,
4
4
"description" : " Angular library for Web Serial API with RxJS" ,
5
5
"repository" : {
6
6
"type" : " git" ,
17
17
"start" : " ng serve" ,
18
18
"build" : " ng build" ,
19
19
"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"
21
23
},
22
24
"private" : false ,
23
25
"dependencies" : {
Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " angular-web-serial" ,
3
- "version" : " 0.0.5 " ,
3
+ "version" : " 0.0.6 " ,
4
4
"peerDependencies" : {
5
5
"@angular/common" : " ^17.3.0" ,
6
6
"@angular/core" : " ^17.3.0"
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 number Diff line number Diff line change 1
1
import { TestBed } from '@angular/core/testing' ;
2
2
3
3
import { AngularSerialService } from './angular-serial.service' ;
4
+ import { AngularSerialTestingModule } from './angular-serial-testing.module' ;
5
+ import { switchMap } from 'rxjs' ;
4
6
5
7
describe ( 'AngularSerialService' , ( ) => {
6
8
let service : AngularSerialService ;
7
9
8
10
beforeEach ( ( ) => {
9
- TestBed . configureTestingModule ( { } ) ;
11
+ TestBed . configureTestingModule ( {
12
+ imports : [ AngularSerialTestingModule ]
13
+ } ) ;
10
14
service = TestBed . inject ( AngularSerialService ) ;
11
15
} ) ;
12
16
13
17
it ( 'should be created' , ( ) => {
14
18
expect ( service ) . toBeTruthy ( ) ;
15
19
} ) ;
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
+
16
45
} ) ;
Original file line number Diff line number Diff line change @@ -12,12 +12,9 @@ import {
12
12
} from 'rxjs' ;
13
13
import { DOCUMENT } from '@angular/common' ;
14
14
15
- @Injectable ( {
16
- providedIn : 'root'
17
- } )
15
+ @Injectable ( )
18
16
export class AngularSerialService {
19
17
20
- readonly serial : Serial | undefined ;
21
18
private port : SerialPort | null = null ;
22
19
private abortController : AbortController | null = null ;
23
20
private dataStream : WritableStream | null = null ;
@@ -34,10 +31,9 @@ export class AngularSerialService {
34
31
}
35
32
36
33
constructor (
37
- @Inject ( DOCUMENT ) private document : Document ,
34
+ @Inject ( 'Serial' ) readonly serial : Serial | undefined ,
38
35
private ngZone : NgZone
39
36
) {
40
- this . serial = this . document ?. defaultView ?. navigator ?. serial ;
41
37
}
42
38
43
39
/**
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments