File tree Expand file tree Collapse file tree 4 files changed +66
-3
lines changed Expand file tree Collapse file tree 4 files changed +66
-3
lines changed Original file line number Diff line number Diff line change 9
9
[ ![ ] ( https://github.com/jpoehnelt/in-solidarity-bot/raw/main/static//badge-flat.png )] ( https://github.com/apps/in-solidarity )
10
10
[ ![ Discord] ( https://img.shields.io/discord/676948200904589322?color=6A7EC2&logo=discord&logoColor=ffffff )] ( https://discord.gg/jRteCzP )
11
11
12
-
13
12
## Description
14
13
15
14
Jest mocks for Google Maps in TypeScript.
@@ -23,12 +22,16 @@ Available via NPM as the package `@googlemaps/jest-mocks`
23
22
## Example
24
23
25
24
``` typescript
26
- import { initialize } from " @googlemaps/jest-mocks" ;
25
+ import { initialize , Map } from " @googlemaps/jest-mocks" ;
27
26
28
27
beforeEach (() => {
29
28
initialize ();
30
29
});
31
30
31
+ // access the object instances if the object isn't easily accessible
32
+ test (" my test" , () => {
33
+ expect (Map .mockInstances [0 ].fitBounds ).toHaveBeenCalled ();
34
+ });
32
35
```
33
36
34
37
## Support
Original file line number Diff line number Diff line change
1
+ import { initialize } from "../../index" ;
2
+ import { Map_ } from "../maps/map" ;
3
+ import { MVCObject } from "./mvcobject" ;
4
+
5
+ test ( "instances are stored" , ( ) => {
6
+ const mvcObject = new MVCObject ( ) ;
7
+ expect ( MVCObject . mockInstances ) . toStrictEqual ( [ mvcObject ] ) ;
8
+ expect ( MVCObject . mockInstances [ 0 ] . addListener ) . toBeTruthy ( ) ;
9
+ } ) ;
10
+
11
+ test ( "setup child class" , ( ) => {
12
+ initialize ( ) ;
13
+ new google . maps . Map ( null ) ;
14
+ expect ( Map_ . mockInstances ) . toBeTruthy ( ) ;
15
+ } ) ;
16
+
17
+ test ( "auto cleanup after each test from above" , ( ) => {
18
+ expect ( MVCObject . mockInstances ) . toBeUndefined ( ) ;
19
+ expect ( Map_ . mockInstances ) . toBeUndefined ( ) ;
20
+ expect ( MVCObject . _mockClasses ) . toBeUndefined ( ) ;
21
+ } ) ;
Original file line number Diff line number Diff line change 17
17
/* eslint-disable @typescript-eslint/no-explicit-any */
18
18
19
19
export class MVCObject implements google . maps . MVCObject {
20
+ public static _mockClasses : typeof MVCObject [ ] = [ ] ;
21
+ public static mockInstances : MVCObject [ ] = [ ] ;
22
+
23
+ public constructor ( ) {
24
+ const ctor = this . constructor as typeof MVCObject ;
25
+
26
+ if ( ctor . mockInstances === undefined ) {
27
+ ctor . mockInstances = [ ] ;
28
+ }
29
+
30
+ if ( MVCObject . _mockClasses === undefined ) {
31
+ MVCObject . _mockClasses = [ ] ;
32
+ }
33
+
34
+ ctor . mockInstances . push ( this ) ;
35
+ MVCObject . _mockClasses . push ( ctor ) ;
36
+ }
37
+
20
38
public addListener = jest
21
39
. fn ( )
22
40
. mockImplementation (
@@ -42,3 +60,16 @@ export class MVCObject implements google.maps.MVCObject {
42
60
public unbind = jest . fn ( ) . mockImplementation ( ( key : string ) : void => null ) ;
43
61
public unbindAll = jest . fn ( ) . mockImplementation ( ( ) => null ) ;
44
62
}
63
+
64
+ // if running a test that supports afterEach, then we will cleanup the instances
65
+ // automatically at the end of each test.
66
+ if ( typeof afterEach === "function" ) {
67
+ afterEach ( ( ) => {
68
+ if ( MVCObject . _mockClasses ) {
69
+ for ( const ctor of MVCObject . _mockClasses ) {
70
+ ctor . mockInstances = undefined ;
71
+ }
72
+ }
73
+ MVCObject . _mockClasses = undefined ;
74
+ } ) ;
75
+ }
Original file line number Diff line number Diff line change 16
16
17
17
import { initialize } from "../../index" ;
18
18
import { ControlPosition } from "../controls/controlposition" ;
19
+ import { Map_ } from "./map" ;
19
20
20
21
test ( "can initialize" , ( ) => {
21
22
initialize ( ) ;
22
23
expect ( new google . maps . Map ( null ) ) . toBeTruthy ( ) ;
23
24
} ) ;
24
25
25
- test ( "controls initalized " , ( ) => {
26
+ test ( "controls initialized " , ( ) => {
26
27
initialize ( ) ;
27
28
const map = new google . maps . Map ( null ) ;
28
29
expect ( map . controls [ ControlPosition . BOTTOM_CENTER ] ) . toBeTruthy ( ) ;
29
30
} ) ;
31
+
32
+ test ( "mockInstances available" , ( ) => {
33
+ initialize ( ) ;
34
+ new google . maps . MVCObject ( ) ;
35
+ const map = new google . maps . Map ( null ) ;
36
+ expect ( Map_ . mockInstances ) . toMatchObject ( [ map ] ) ;
37
+ } ) ;
You can’t perform that action at this time.
0 commit comments