@@ -4,6 +4,7 @@ const API_KEY = 'test';
44
55describe ( 'constructor()' , ( ) => {
66 it ( 'should throw an error if an apiKey is not provided' , ( ) => {
7+ // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
78 // @ts -ignore
89 expect ( ( ) => new IPData ( ) ) . toThrowError ( 'An API key is required.' ) ;
910 } ) ;
@@ -12,28 +13,22 @@ describe('constructor()', () => {
1213 const ipdata = new IPData ( API_KEY ) ;
1314 expect ( ipdata . apiKey ) . toEqual ( API_KEY ) ;
1415 } ) ;
15-
16- it ( 'should turn on the cache by default' , ( ) => {
17- const ipdata = new IPData ( API_KEY ) ;
18- expect ( ipdata . useCache ) . toEqual ( true ) ;
19- } ) ;
20-
21- it ( 'should turn off the cache' , ( ) => {
22- const ipdata = new IPData ( API_KEY , false ) ;
23- expect ( ipdata . useCache ) . toEqual ( false ) ;
24- } ) ;
2516} )
2617
2718describe ( 'lookup()' , ( ) => {
28- const ipdata = new IPData ( API_KEY , false ) ;
19+ const ipdata = new IPData ( API_KEY ) ;
2920 const IP = '1.1.1.1' ;
3021
31- it ( 'should throw an error if the ip is invalid' , async ( ) => {
22+ afterEach ( ( ) => {
23+ return ipdata . cache . reset ( ) ;
24+ } ) ;
25+
26+ it ( 'should throw an error if the ip address is invalid' , async ( ) => {
3227 const badIP = '1.1.11' ;
3328 await expect ( ipdata . lookup ( badIP ) ) . rejects . toThrowError ( `${ badIP } is an invalid IP address.` ) ;
3429 } ) ;
3530
36- it ( 'should return default information when no ip is provided' , async ( ) => {
31+ it ( 'should return default information when no ip address is provided' , async ( ) => {
3732 const info = await ipdata . lookup ( ) ;
3833 expect ( info ) . toHaveProperty ( 'ip' ) ;
3934 expect ( info ) . toHaveProperty ( 'status' ) ;
@@ -51,12 +46,26 @@ describe('lookup()', () => {
5146 expect ( info ) . toHaveProperty ( 'status' ) ;
5247 } ) ;
5348
54- it ( 'should set an ip in the uri' , async ( ) => {
49+ it ( 'should cache the ip address info for the default ip address' , async ( ) => {
50+ await ipdata . lookup ( ) ;
51+ const info = ipdata . cache . get ( 'DEFAULT_IP' ) ;
52+ expect ( info ) . toHaveProperty ( 'ip' ) ;
53+ expect ( info ) . toHaveProperty ( 'status' ) ;
54+ } ) ;
55+
56+ it ( 'should accept an ip address' , async ( ) => {
5557 const info = await ipdata . lookup ( IP ) ;
5658 expect ( info ) . toHaveProperty ( 'ip' , IP ) ;
5759 expect ( info ) . toHaveProperty ( 'status' ) ;
5860 } ) ;
5961
62+ it ( 'should cache the ip address info' , async ( ) => {
63+ await ipdata . lookup ( IP ) ;
64+ const info = ipdata . cache . get ( IP ) ;
65+ expect ( info ) . toHaveProperty ( 'ip' , IP ) ;
66+ expect ( info ) . toHaveProperty ( 'status' ) ;
67+ } ) ;
68+
6069 it ( 'should throw an error if a selectField and fields is provided' , async ( ) => {
6170 await expect ( ipdata . lookup ( null , 'field' , [ 'field' ] ) ) . rejects . toThrowError (
6271 'The selectField and fields parameters cannot be used at the same time.' ,
@@ -97,24 +106,28 @@ describe('lookup()', () => {
97106} ) ;
98107
99108describe ( 'bulkLookup()' , ( ) => {
100- const ipdata = new IPData ( API_KEY , false ) ;
109+ const ipdata = new IPData ( API_KEY ) ;
101110 const IP1 = '1.1.1.1' ;
102111 const IP2 = '8.8.8.8' ;
103112
104- it ( 'should throw an error if less than 2 ips are provided' , async ( ) => {
113+ afterEach ( ( ) => {
114+ return ipdata . cache . reset ( ) ;
115+ } ) ;
116+
117+ it ( 'should throw an error if less than 2 ip addresses are provided' , async ( ) => {
105118 const ips = [ IP1 ] ;
106119 await expect ( ipdata . bulkLookup ( ips ) ) . rejects . toThrowError (
107120 'Bulk Lookup requires more than 1 IP Address in the payload.' ,
108121 ) ;
109122 } ) ;
110123
111- it ( 'should throw an error if an ip is invalid' , async ( ) => {
124+ it ( 'should throw an error if an ip address is invalid' , async ( ) => {
112125 const badIP = '1.1.11' ;
113126 const ips = [ badIP , IP2 ] ;
114127 await expect ( ipdata . bulkLookup ( ips ) ) . rejects . toThrowError ( `${ badIP } is an invalid IP address.` ) ;
115128 } ) ;
116129
117- it ( 'should return info for the ips ' , async ( ) => {
130+ it ( 'should return info for the ip addresses ' , async ( ) => {
118131 const info = await ipdata . bulkLookup ( [ IP1 , IP2 ] ) ;
119132 expect ( info ) . toHaveProperty ( 'responses' ) ;
120133 expect ( info ) . toHaveProperty ( 'status' ) ;
0 commit comments