Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor-Vuk committed Oct 3, 2024
1 parent 304e251 commit 3fe8d42
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"leva": "^0.9.35",
"lucide-react": "^0.447.0",
"mapbox-gl": "^3.7.0",
"r3f-perf": "^7.2.1",
"r3f-perf": "^7.2.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-map-gl": "^7.1.7",
Expand Down
43 changes: 43 additions & 0 deletions src/contentComponents/regularComponents/Menu/menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,49 @@ describe("Menu Component", () => {
})
})

it("should throw error when Botinec ic clicked and there is no ref.current", async () => {
/* since we are gonna exchange here mapRef with out mocked version, we will not use MenuMock so we need to setup stuff here */
const user = userEvent.setup()

/* vi.spyOn() is used to spy on existing methods or functions of objects. It allows you to monitor and control what happens
when the method is called, but the original method remains intact unless explicitly overridden with mockImplementation() */
const consoleWarnSpy = vi
.spyOn(console, "warn")
.mockImplementation(() => {})

const mapRefMock = {
current: null,
} as Partial<MapRef> // telling TypeScript that this mock object will only have some properties of the full MapRef interface, and others can be omitted

// Replace mapRef with a mock object
render(<Menu mapRef={mapRefMock as RefObject<MapRef>} />)

const menuButton = screen.getByRole("button", { name: /Menu/i })

await user.click(menuButton)

const dropdownNavigationButton = await screen.findByRole("button", {
name: /Navigation/i,
})

await user.click(dropdownNavigationButton)

const dropdownBotinecButton = await screen.findByRole("button", {
name: /Botinec/i,
})

await user.click(dropdownBotinecButton)

// Ensure that flyTo method was called once
expect(consoleWarnSpy).toHaveBeenCalledWith(
"Map instance is not available!",
)

/* When you use spyOn, it alters the real method to track its usage. We need to call mockRestore() to restore the method to its original state after the test
because it could persist and interfere with other tests.*/
consoleWarnSpy.mockRestore()
})

it("should open toast when click on 'i' button", async () => {
// Toaster component is in Experience.tsx not Menu.tsx so we must render it here again in order to show up

Expand Down

0 comments on commit 3fe8d42

Please sign in to comment.