@@ -2,33 +2,41 @@ import BaseCrud from "../resources/BaseCrud";
22import BillsV4 from "../resources/BillsV4" ;
33import Chance from "chance" ;
44import { BillsV4Static } from "../interfaces/BillsV4Static" ;
5-
6- jest . mock ( "../resources/BaseCrud" ) ;
5+ import { BaseStatic } from "../interfaces/BaseStatic" ;
76
87const seedgenerator = new Chance ( ) ;
98const seed = seedgenerator . hash ( ) ;
109console . log ( `using chance seed ${ seed } ` ) ;
1110const chance = new Chance ( seed ) ;
1211
1312describe ( "BillsV4" , ( ) => {
13+ let requestSpy : jest . SpyInstance ;
14+
15+ beforeEach ( ( ) => {
16+ requestSpy = jest
17+ . spyOn ( BaseCrud . prototype as any , "request" )
18+ . mockResolvedValue ( { } ) ;
19+ } ) ;
20+
1421 afterEach ( ( ) => {
15- jest . resetAllMocks ( ) ;
22+ requestSpy . mockRestore ( ) ;
1623 } ) ;
1724
1825 it ( "Should use init the base correctly" , ( ) => {
1926 const token = chance . string ( ) ;
20- new BillsV4 ( token ) ;
21- expect ( BaseCrud ) . toHaveBeenCalledWith ( token , "/4.0/purchase/bills" ) ;
27+ const billsV4 = new BillsV4 ( token ) ;
28+ // @ts -ignore
29+ expect ( billsV4 . apiToken ) . toBe ( token ) ;
30+ // @ts -ignore
31+ expect ( billsV4 . apiEndpoint ) . toBe ( "/4.0/purchase/bills" ) ;
2232 } ) ;
2333
2434 describe ( "list" , ( ) => {
2535 it ( "Should call request with GET" , async ( ) => {
2636 const billsV4 = new BillsV4 ( chance . string ( ) ) ;
27- // @ts -ignore
28- BaseCrud . prototype . request . mockResolvedValue ( { data : [ ] } ) ;
37+ requestSpy . mockResolvedValue ( { data : [ ] } ) ;
2938 await billsV4 . list ( ) ;
30- // @ts -ignore
31- expect ( BaseCrud . prototype . request ) . toHaveBeenCalledWith (
39+ expect ( requestSpy ) . toHaveBeenCalledWith (
3240 "GET" ,
3341 "/4.0/purchase/bills" ,
3442 undefined
@@ -37,23 +45,87 @@ describe("BillsV4", () => {
3745
3846 it ( "Should pass options to request" , async ( ) => {
3947 const billsV4 = new BillsV4 ( chance . string ( ) ) ;
40- const options : BillsV4Static . ListOptions = { limit : chance . integer ( { min : 0 } ) } ;
41- // @ts -ignore
42- BaseCrud . prototype . request . mockResolvedValue ( { data : [ ] } ) ;
48+ const options : BaseStatic . BaseOptions = { limit : chance . integer ( ) } ;
49+ requestSpy . mockResolvedValue ( { data : [ ] } ) ;
4350 await billsV4 . list ( options ) ;
44- // @ts -ignore
45- expect ( BaseCrud . prototype . request ) . toHaveBeenCalledWith (
51+ expect ( requestSpy ) . toHaveBeenCalledWith (
52+ "GET" ,
53+ "/4.0/purchase/bills" ,
54+ options
55+ ) ;
56+ } ) ;
57+
58+ it ( "should list bills with options" , async ( ) => {
59+ const billsV4 = new BillsV4 ( chance . string ( ) ) ;
60+ const bills = [
61+ { id : chance . guid ( ) , document_no : "B-1" , lastname_company : "Test" } ,
62+ ] ;
63+ requestSpy . mockResolvedValue ( { data : bills } ) ;
64+
65+ const options : BillsV4Static . ListOptions = {
66+ limit : 10 ,
67+ page : 1 ,
68+ "fields[]" : [ "document_no" , "title" ] ,
69+ } ;
70+ const result = await billsV4 . list ( options ) ;
71+
72+ expect ( requestSpy ) . toHaveBeenCalledWith (
4673 "GET" ,
4774 "/4.0/purchase/bills" ,
4875 options
4976 ) ;
77+ expect ( result ) . toEqual ( bills ) ;
78+ } ) ;
79+ } ) ;
80+
81+ describe ( "updateStatus" , ( ) => {
82+ it ( "Should call request with PUT and correct path" , async ( ) => {
83+ const billsV4 = new BillsV4 ( chance . string ( ) ) ;
84+ const id = chance . guid ( ) ;
85+ const status = BillsV4Static . BillStatusUpdate . BOOKED ;
86+ await billsV4 . updateStatus ( id , status ) ;
87+ expect ( requestSpy ) . toHaveBeenCalledWith (
88+ "PUT" ,
89+ `/4.0/purchase/bills/${ id } /bookings/${ status } `
90+ ) ;
91+ } ) ;
92+ } ) ;
93+
94+ describe ( "executeAction" , ( ) => {
95+ it ( "Should call request with POST and correct path and data" , async ( ) => {
96+ const billsV4 = new BillsV4 ( chance . string ( ) ) ;
97+ const id = chance . guid ( ) ;
98+ const action = BillsV4Static . BillAction . DUPLICATE ;
99+ await billsV4 . executeAction ( id , action ) ;
100+ expect ( requestSpy ) . toHaveBeenCalledWith (
101+ "POST" ,
102+ `/4.0/purchase/bills/${ id } /actions` ,
103+ undefined ,
104+ { action }
105+ ) ;
106+ } ) ;
107+ } ) ;
108+
109+ describe ( "validateDocumentNumber" , ( ) => {
110+ it ( "Should call request with GET and correct path and data" , async ( ) => {
111+ const billsV4 = new BillsV4 ( chance . string ( ) ) ;
112+ const documentNo = chance . string ( ) ;
113+ requestSpy . mockResolvedValue ( { valid : true } ) ;
114+ await billsV4 . validateDocumentNumber ( documentNo ) ;
115+ expect ( requestSpy ) . toHaveBeenCalledWith (
116+ "GET" ,
117+ "/4.0/purchase/documentnumbers/bills" ,
118+ { document_no : documentNo }
119+ ) ;
50120 } ) ;
51121 } ) ;
52122
53123 describe ( "search" , ( ) => {
54124 it ( "Should throw not implemented error" , async ( ) => {
55125 const billsV4 = new BillsV4 ( chance . string ( ) ) ;
56- await expect ( billsV4 . search ( [ ] ) ) . rejects . toThrow ( "not implemented by Bexio yet" ) ;
126+ await expect ( billsV4 . search ( [ ] ) ) . rejects . toThrow (
127+ "not implemented by Bexio yet"
128+ ) ;
57129 } ) ;
58130 } ) ;
59131
@@ -66,7 +138,7 @@ describe("BillsV4", () => {
66138 // @ts -ignore
67139 expect ( BaseCrud . prototype . request ) . toHaveBeenCalledWith (
68140 "PUT" ,
69- `undefined /${ id } /bookings/${ status } `
141+ `/4.0/purchase/bills /${ id } /bookings/${ status } `
70142 ) ;
71143 } ) ;
72144 } ) ;
@@ -80,7 +152,7 @@ describe("BillsV4", () => {
80152 // @ts -ignore
81153 expect ( BaseCrud . prototype . request ) . toHaveBeenCalledWith (
82154 "POST" ,
83- `undefined /${ id } /actions` ,
155+ `/4.0/purchase/bills /${ id } /actions` ,
84156 undefined ,
85157 { action }
86158 ) ;
0 commit comments