@@ -50,20 +50,7 @@ describe("JSON Browser", () => {
5050 } ) ;
5151 } ) ;
5252
53- test ( "length of array" , async ( ) => {
54- const jref = `[42]` ;
55-
56- mockAgent . get ( testDomain )
57- . intercept ( { method : "GET" , path : "/foo" } )
58- . reply ( 200 , jref , { headers : { "content-type" : "application/reference+json" } } ) ;
59-
60- const hyperjump = new Hyperjump ( ) ;
61- const subject = await hyperjump . get ( `${ testDomain } /foo` ) ;
62-
63- expect ( hyperjump . length ( subject ) ) . to . eql ( 1 ) ;
64- } ) ;
65-
66- describe ( "typeOf" , ( ) => {
53+ describe ( "typeOf/value and type narrowing" , ( ) => {
6754 const hyperjump = new Hyperjump ( ) ;
6855
6956 beforeEach ( ( ) => {
@@ -83,38 +70,70 @@ describe("JSON Browser", () => {
8370 } ) ;
8471
8572 test ( "null" , async ( ) => {
86- const subject = await hyperjump . get ( `${ testDomain } /foo#/null` ) ;
87- expect ( hyperjump . typeOf ( subject ) ) . to . eql ( "null" ) ;
73+ const node = await hyperjump . get ( `${ testDomain } /foo#/null` ) ;
74+ if ( hyperjump . typeOf ( node , "null" ) ) {
75+ /** @type null */
76+ const subject = hyperjump . value ( node ) ;
77+ expect ( subject ) . to . equal ( null ) ;
78+ } else {
79+ expect . fail ( ) ;
80+ }
8881 } ) ;
8982
9083 test ( "true" , async ( ) => {
91- const subject = await hyperjump . get ( `${ testDomain } /foo#/true` ) ;
92- expect ( hyperjump . typeOf ( subject ) ) . to . eql ( "boolean" ) ;
84+ const node = await hyperjump . get ( `${ testDomain } /foo#/true` ) ;
85+ if ( hyperjump . typeOf ( node , "boolean" ) ) {
86+ /** @type boolean */
87+ const subject = hyperjump . value ( node ) ;
88+ expect ( subject ) . to . equal ( true ) ;
89+ } else {
90+ expect . fail ( ) ;
91+ }
9392 } ) ;
9493
9594 test ( "false" , async ( ) => {
96- const subject = await hyperjump . get ( `${ testDomain } /foo#/false` ) ;
97- expect ( hyperjump . typeOf ( subject ) ) . to . eql ( "boolean" ) ;
95+ const node = await hyperjump . get ( `${ testDomain } /foo#/false` ) ;
96+ if ( hyperjump . typeOf ( node , "boolean" ) ) {
97+ /** @type boolean */
98+ const subject = hyperjump . value ( node ) ;
99+ expect ( subject ) . to . equal ( false ) ;
100+ } else {
101+ expect . fail ( ) ;
102+ }
98103 } ) ;
99104
100105 test ( "number" , async ( ) => {
101- const subject = await hyperjump . get ( `${ testDomain } /foo#/number` ) ;
102- expect ( hyperjump . typeOf ( subject ) ) . to . eql ( "number" ) ;
106+ const node = await hyperjump . get ( `${ testDomain } /foo#/number` ) ;
107+ if ( hyperjump . typeOf ( node , "number" ) ) {
108+ /** @type number */
109+ const subject = hyperjump . value ( node ) ;
110+ expect ( subject ) . to . equal ( 42 ) ;
111+ } else {
112+ expect . fail ( ) ;
113+ }
103114 } ) ;
104115
105116 test ( "string" , async ( ) => {
106- const subject = await hyperjump . get ( `${ testDomain } /foo#/string` ) ;
107- expect ( hyperjump . typeOf ( subject ) ) . to . eql ( "string" ) ;
117+ const node = await hyperjump . get ( `${ testDomain } /foo#/string` ) ;
118+ if ( hyperjump . typeOf ( node , "string" ) ) {
119+ /** @type string */
120+ const subject = hyperjump . value ( node ) ;
121+ expect ( subject ) . to . equal ( "foo" ) ;
122+ } else {
123+ expect . fail ( ) ;
124+ }
108125 } ) ;
109126
110127 test ( "array" , async ( ) => {
111- const subject = await hyperjump . get ( `${ testDomain } /foo#/array` ) ;
112- expect ( hyperjump . typeOf ( subject ) ) . to . eql ( "array" ) ;
128+ const node = await hyperjump . get ( `${ testDomain } /foo#/array` ) ;
129+ expect ( hyperjump . typeOf ( node , "array" ) ) . to . equal ( true ) ;
130+ expect ( ( ) => hyperjump . value ( node ) ) . to . throw ( ) ;
113131 } ) ;
114132
115133 test ( "object" , async ( ) => {
116- const subject = await hyperjump . get ( `${ testDomain } /foo#/object` ) ;
117- expect ( hyperjump . typeOf ( subject ) ) . to . eql ( "object" ) ;
134+ const node = await hyperjump . get ( `${ testDomain } /foo#/object` ) ;
135+ expect ( hyperjump . typeOf ( node , "object" ) ) . to . equal ( true ) ;
136+ expect ( ( ) => hyperjump . value ( node ) ) . to . throw ( ) ;
118137 } ) ;
119138 } ) ;
120139} ) ;
0 commit comments