File tree 2 files changed +51
-3
lines changed
unit-testing/chapter-examples/transmission-processor/tests
2 files changed +51
-3
lines changed Original file line number Diff line number Diff line change
1
+ function processor ( transmission ) {
2
+ if ( transmission . indexOf ( "::" ) < 0 ) {
3
+ // Data is invalid
4
+ return - 1 ;
5
+ }
6
+ let parts = transmission . split ( "::" ) ;
7
+ let rawData = parts [ 1 ] ;
8
+ if ( rawData [ 0 ] !== "<" ) {
9
+ rawData = - 1 ;
10
+ }
11
+ return {
12
+ id : Number ( parts [ 0 ] ) ,
13
+ rawData : rawData
14
+ } ;
15
+ }
16
+
17
+ module . exports = processor ;
Original file line number Diff line number Diff line change
1
+ const processor = require ( './processor.js' ) ;
2
+
1
3
describe ( "transmission processor" , function ( ) {
2
4
3
- // TODO: put tests here
4
-
5
- } ) ;
5
+ test ( "takes a string returns an object" , function ( ) {
6
+ let result = processor ( "9701::<489584872710>" ) ;
7
+ expect ( typeof result ) . toBe ( "object" ) ;
8
+ } ) ;
9
+
10
+ test ( "returns -1 if '::' not found" , function ( ) {
11
+ let result = processor ( "9701<489584872710>" ) ;
12
+ expect ( result ) . toBe ( - 1 ) ;
13
+ } ) ;
14
+
15
+ test ( "returns id in object" , function ( ) {
16
+ let result = processor ( "9701::<489584872710>" ) ;
17
+ expect ( result . id ) . not . toBeUndefined ( ) ;
18
+ } ) ;
19
+
20
+ test ( "converts id to a number" , function ( ) {
21
+ let result = processor ( "9701::<489584872710>" ) ;
22
+ expect ( result . id ) . toBe ( 9701 ) ;
23
+ } ) ;
24
+
25
+ test ( "returns rawData in object" , ( ) => {
26
+ let result = processor ( "9701::<487297403495720912>" ) ;
27
+ expect ( result . rawData ) . not . toBeUndefined ( ) ;
28
+ } ) ;
29
+
30
+ test ( "returns -1 for rawData if missing < at position 0" , function ( ) {
31
+ let result = processor ( "9701::487297403495720912>" ) ;
32
+ expect ( result . rawData ) . toBe ( - 1 ) ;
33
+ } ) ;
34
+
35
+
36
+ } ) ;
You can’t perform that action at this time.
0 commit comments