Skip to content

Commit 40db691

Browse files
authored
feat: implement mocks registry (#194)
1 parent f9b10c2 commit 40db691

39 files changed

+250
-40
lines changed

README.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,56 @@ Jest mocks for Google Maps in TypeScript.
1919

2020
Available via NPM as the package `@googlemaps/jest-mocks`
2121

22-
## Example
22+
## Inspecting mocks
2323

24-
```typescript
25-
import { initialize, Map } from "@googlemaps/jest-mocks";
24+
You can inspect what happens with the created mock instances (e.g. `Map` or `Marker`) via the `mockInstances` object.
25+
26+
```ts
27+
import { initialize, Map, Marker, mockInstances } from "@googlemaps/jest-mocks";
2628

2729
beforeEach(() => {
2830
initialize();
2931
});
3032

31-
// access the object instances if the object isn't easily accessible
3233
test("my test", () => {
33-
expect(Map.mockInstances[0].fitBounds).toHaveBeenCalled();
34+
const map = new google.maps.Map(null);
35+
const markerOne = new google.maps.Marker();
36+
const markerTwo = new google.maps.Marker();
37+
38+
map.setHeading(8);
39+
markerOne.setMap(map);
40+
markerTwo.setLabel("My marker");
41+
42+
const mapMocks = mockInstances.get(Map);
43+
const markerMocks = mockInstances.get(Marker);
44+
45+
expect(mapMocks).toHaveLength(1);
46+
expect(markerMocks).toHaveLength(2);
47+
expect(mapMocks[0].setHeading).toHaveBeenCalledWith(8);
48+
expect(markerMocks[0].setMap).toHaveBeenCalledTimes(1);
49+
expect(markerMocks[1].setLabel).toHaveBeenCalledWith("My marker");
50+
});
51+
```
52+
53+
## Cleaning up mocks
54+
55+
Whenever `initialize()` is called, the captured mocks are automatically cleaned. Using any of Jest's methods, you can clean the mock instances at any time:
56+
57+
```ts
58+
import { initialize, Map, Marker, mockInstances } from "@googlemaps/jest-mocks";
59+
60+
beforeAll(() => {
61+
initialize();
62+
});
63+
64+
// Clear all mocks
65+
beforeEach(() => {
66+
mockInstances.clearAll();
67+
});
68+
69+
// Clear specific mocks
70+
beforeEach(() => {
71+
mockInstances.clear(Map, Marker);
3472
});
3573
```
3674

src/drawing/DOM/mapcanvasprojection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 Google LLC. All Rights Reserved.
2+
* Copyright 2022 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/drawing/DOM/mappanes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 Google LLC. All Rights Reserved.
2+
* Copyright 2022 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/drawing/DOM/overlayview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 Google LLC. All Rights Reserved.
2+
* Copyright 2022 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/drawing/data/data.feature.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 Google LLC. All Rights Reserved.
2+
* Copyright 2022 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/drawing/data/data.feature.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 Google LLC. All Rights Reserved.
2+
* Copyright 2022 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/drawing/data/data.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 Google LLC. All Rights Reserved.
2+
* Copyright 2022 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/drawing/data/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 Google LLC. All Rights Reserved.
2+
* Copyright 2022 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/drawing/marker/marker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 Google LLC. All Rights Reserved.
2+
* Copyright 2022 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/drawing/polygons/circle.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019 Google LLC. All Rights Reserved.
2+
* Copyright 2022 Google LLC. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)