1+ import fetchMock from '@fetch-mock/jest' ;
2+
3+ import { NetworkAsCodeClient } from "../src" ;
4+ import { Device } from "../src/models/device" ;
5+ import { InvalidParameterError } from "../src/errors" ;
6+
7+ jest . mock ( "node-fetch" , ( ) => {
8+ const nodeFetch = jest . requireActual ( "node-fetch" ) ;
9+ // only needed if your application makes use of Response, Request
10+ // or Headers classes directly
11+ Object . assign ( fetchMock . config , {
12+ fetch : nodeFetch ,
13+ Response : nodeFetch . Response ,
14+ Request : nodeFetch . Request ,
15+ Headers : nodeFetch . Headers
16+ } ) ;
17+ return fetchMock . fetchHandler ;
18+ } ) ;
19+
20+ let client : NetworkAsCodeClient ;
21+
22+ let device : Device ;
23+
24+ beforeAll ( ( ) : any => {
25+ client = new NetworkAsCodeClient ( "TEST_TOKEN" ) ;
26+ device = client . devices . get ( {
27+ phoneNumber : "+999999991000"
28+ } ) ;
29+ return client ;
30+ } ) ;
31+
32+ beforeEach ( ( ) => {
33+ fetchMock . mockReset ( ) ;
34+ } ) ;
35+
36+ afterEach ( ( ) => {
37+ fetchMock . unmockGlobal ( ) ;
38+ } ) ;
39+
40+ describe ( "Call Forwarding Signal" , ( ) => {
41+ it ( "should verify unconditional call forwarding" , async ( ) => {
42+ fetchMock . mockGlobal ( ) . post (
43+ "https://network-as-code.p-eu.rapidapi.com/passthrough/camara/v1/call-forwarding-signal/call-forwarding-signal/v0.3/unconditional-call-forwardings" ,
44+ ( _ : any , req : any ) : any => {
45+ expect ( JSON . parse ( req . body . toString ( ) ) ) . toEqual ( {
46+ phoneNumber : "+999999991000"
47+ } ) ;
48+ } ,
49+ { response : Promise . resolve ( {
50+ body : JSON . stringify ( {
51+ active : true
52+ } )
53+ } )
54+ }
55+ ) ;
56+
57+ const result = await device . verifyUnconditionalForwarding ( ) ;
58+ expect ( result ) . toBe ( true ) ;
59+ } ) ;
60+
61+ it ( "should get list of call forwarding services" , async ( ) => {
62+ fetchMock . mockGlobal ( ) . post (
63+ "https://network-as-code.p-eu.rapidapi.com/passthrough/camara/v1/call-forwarding-signal/call-forwarding-signal/v0.3/call-forwardings" ,
64+ ( _ : any , req : any ) : any => {
65+ expect ( JSON . parse ( req . body . toString ( ) ) ) . toEqual ( {
66+ phoneNumber : "+999999991000"
67+ } ) ;
68+ } ,
69+ { response : Promise . resolve ( {
70+ body : JSON . stringify ( [
71+ "unconditional" ,
72+ "conditional_busy" ,
73+ "conditional_no_answer"
74+ ] )
75+ } )
76+ }
77+ ) ;
78+
79+ const result = await device . getCallForwarding ( ) ;
80+ let types = [ 'inactive' , 'unconditional' , 'conditional_busy' , 'conditional_not_reachable' , 'conditional_no_answer' ]
81+
82+ expect ( result instanceof Array ) . toBeTruthy ( ) ;
83+ expect ( result . every ( ( val ) => types . includes ( val ) ) ) ;
84+ } ) ;
85+
86+ it ( "should raise exception if verifying forwarding without phone number" , async ( ) => {
87+ fetchMock . mockGlobal ( ) . post (
88+ "https://network-as-code.p-eu.rapidapi.com/passthrough/camara/v1/call-forwarding-signal/call-forwarding-signal/v0.3/unconditional-call-forwardings" ,
89+ ( _ : any , req : any ) : any => {
90+ expect ( JSON . parse ( req . body . toString ( ) ) ) . toEqual ( {
91+ networkAccessIdentifier : "device@testcsp.net"
92+ } ) ;
93+ }
94+ ) ;
95+
96+ const deviceWithoutNumber = client . devices . get ( {
97+ networkAccessIdentifier : "device@testcsp.net"
98+ } ) ;
99+
100+ expect (
101+ async ( ) => await deviceWithoutNumber . verifyUnconditionalForwarding ( )
102+ ) . rejects . toThrow ( InvalidParameterError ) ;
103+ } ) ;
104+
105+ it ( "should raise exception if getting forwarding services without phone number" , async ( ) => {
106+ fetchMock . mockGlobal ( ) . post (
107+ "https://network-as-code.p-eu.rapidapi.com/passthrough/camara/v1/call-forwarding-signal/call-forwarding-signal/v0.3/call-forwardings" ,
108+ ( _ : any , req : any ) : any => {
109+ expect ( JSON . parse ( req . body . toString ( ) ) ) . toEqual ( {
110+ networkAccessIdentifier : "device@testcsp.net"
111+ } ) ;
112+ }
113+ ) ;
114+
115+ const deviceWithoutNumber = client . devices . get ( {
116+ networkAccessIdentifier : "device@testcsp.net"
117+ } ) ;
118+
119+ expect (
120+ async ( ) => await deviceWithoutNumber . getCallForwarding ( )
121+ ) . rejects . toThrow ( InvalidParameterError ) ;
122+ } ) ;
123+ } ) ;
0 commit comments