-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
244 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"formatter": { | ||
"indentStyle": "space", | ||
"indentSize": 4 | ||
}, | ||
"linter": { | ||
"enabled": true, | ||
"rules": { | ||
"recommended": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
src/lanco-incidents-app/src/hooks/use-web-share.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
|
||
import { act, renderHook, waitFor } from "@testing-library/react"; | ||
import { describe, it, expect, afterEach, vi, beforeEach, Mock } from "vitest"; | ||
|
||
import { useWebShare } from "hooks/use-web-share"; | ||
|
||
describe("useWebShare", () => { | ||
it("initializes", async () => { | ||
// Arrange, Act | ||
const { result } = renderHook(() => { | ||
return useWebShare(); | ||
}); | ||
|
||
// Assert | ||
expect(result.current).toBeDefined(); | ||
expect(result.current).toBeTypeOf("object"); | ||
expect(result.current.isSharing).toBe(false); | ||
expect(result.current.enabled).toBeDefined(); | ||
expect(result.current.error).toBeUndefined(); | ||
expect(result.current.share).toBeTypeOf("function"); | ||
}); | ||
|
||
describe("when no browser sharing capability", () => { | ||
it("is not enabled", async () => { | ||
// Arrange, Act | ||
const { result } = renderHook(() => { | ||
return useWebShare(); | ||
}); | ||
|
||
// Act | ||
const { enabled } = result.current; | ||
|
||
// Assert | ||
expect(enabled).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("when browser has sharing capability", () => { | ||
beforeEach(() => { | ||
vi.stubGlobal("navigator", { share: vi.fn() }); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it("is enabled", async () => { | ||
// Arrange, Act | ||
const { result } = renderHook(() => { | ||
return useWebShare(); | ||
}); | ||
|
||
// Act | ||
const { enabled } = result.current; | ||
|
||
// Assert | ||
expect(enabled).toBe(true); | ||
}); | ||
}); | ||
|
||
describe("share()", () => { | ||
describe("when browser has sharing capability", () => { | ||
afterEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it("calls navigator.share", async () => { | ||
// Arrange | ||
const mockShare: Mock<any[], Promise<void>> = vi.fn(() => | ||
Promise.resolve() | ||
); | ||
vi.stubGlobal("navigator", { share: mockShare }); | ||
const { result } = renderHook(() => { | ||
return useWebShare(); | ||
}); | ||
|
||
// Act | ||
act(() => { | ||
result.current.share({}); | ||
}); | ||
|
||
// Assert | ||
expect(mockShare).toBeCalled(); | ||
}); | ||
|
||
describe("when successful", () => { | ||
it("is updates isSharing", async () => { | ||
// Arrange | ||
vi.stubGlobal("navigator", { | ||
share: () => Promise.resolve(), | ||
}); | ||
const all: ReturnType<typeof useWebShare>[] = []; | ||
const { result, rerender } = renderHook(() => { | ||
const value = useWebShare(); | ||
all.push(value); | ||
return value; | ||
}); | ||
|
||
// Act | ||
act(() => { | ||
result.current.share({}); | ||
}); | ||
|
||
// Assert | ||
expect(result.current.isSharing).toBe(true); | ||
|
||
await waitFor(() => !result.current.isSharing); | ||
rerender(); | ||
|
||
expect(result.current.isSharing).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("when errored", () => { | ||
it("is updates error", async () => { | ||
// Arrange | ||
const errorMessage = "This is an error"; | ||
vi.stubGlobal("navigator", { | ||
share: () => Promise.reject(errorMessage), | ||
}); | ||
const all: ReturnType<typeof useWebShare>[] = []; | ||
const { result, rerender } = renderHook(() => { | ||
const value = useWebShare(); | ||
all.push(value); | ||
return value; | ||
}); | ||
|
||
// Act | ||
act(() => { | ||
result.current.share({}); | ||
}); | ||
|
||
// Assert | ||
expect(result.current.isSharing).toBe(true); | ||
|
||
await waitFor(() => !result.current.isSharing); | ||
rerender(); | ||
|
||
expect(result.current.error).toBe(errorMessage); | ||
expect(result.current.isSharing).toBe(false); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useState } from "react"; | ||
|
||
export function useWebShare() { | ||
const [error, setError] = useState<string>(); | ||
const [isSharing, setIsSharing] = useState(false); | ||
const enabled = window.navigator.share != null; | ||
|
||
const share = ({ title, text, url }: Omit<ShareData, "files">) => { | ||
if (!navigator.share) { | ||
return; | ||
} | ||
|
||
setIsSharing(true); | ||
|
||
navigator | ||
.share({ title, text, url }) | ||
.catch((error) => { | ||
const errorMessage: string = | ||
error instanceof Error | ||
? error.message | ||
: typeof error === "string" | ||
? error | ||
: error?.toString(); | ||
|
||
setError(errorMessage); | ||
}) | ||
.finally(() => setIsSharing(false)); | ||
}; | ||
|
||
return { | ||
isSharing, | ||
enabled, | ||
error, | ||
share, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
316698b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
lanco-live-incidents – ./
lanco-live-incidents.vercel.app
lanco-live-incidents-mytydev.vercel.app
lanco.mytydev.com
lanco-live-incidents-git-main-mytydev.vercel.app