-
Notifications
You must be signed in to change notification settings - Fork 58
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
13 changed files
with
233 additions
and
1 deletion.
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
103 changes: 103 additions & 0 deletions
103
explorer/src/lib/components/__tests__/StaleDataNotice.spec.js
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,103 @@ | ||
import { | ||
afterAll, | ||
afterEach, | ||
beforeEach, | ||
describe, | ||
expect, | ||
it, | ||
vi, | ||
} from "vitest"; | ||
import { act, cleanup, render } from "@testing-library/svelte"; | ||
import { marketDataStore } from "$lib/stores"; | ||
import { StaleDataNotice } from "../"; | ||
|
||
const mockedReadableStore = await vi.hoisted(async () => { | ||
const { mockReadableStore } = await import("$lib/dusk/test-helpers"); | ||
return mockReadableStore({ | ||
data: null, | ||
error: null, | ||
isLoading: false, | ||
lastUpdate: null, | ||
}); | ||
}); | ||
|
||
vi.mock("$lib/stores", async (importOriginal) => { | ||
/** @type {MarketDataStore} */ | ||
const original = await importOriginal(); | ||
|
||
return { | ||
...original, | ||
marketDataStore: { | ||
...mockedReadableStore, | ||
isDataStale: vi.fn(() => false), | ||
}, | ||
}; | ||
}); | ||
|
||
describe("StaleDataNotice", () => { | ||
const initialState = structuredClone( | ||
mockedReadableStore.getMockedStoreValue() | ||
); | ||
|
||
beforeEach(() => { | ||
mockedReadableStore.setMockedStoreValue(initialState); | ||
}); | ||
|
||
afterEach(cleanup); | ||
|
||
afterAll(() => { | ||
vi.doUnmock("$lib/stores"); | ||
}); | ||
|
||
it("does not render the Stale Data notice if data is not stale", () => { | ||
mockedReadableStore.setMockedStoreValue({ | ||
...initialState, | ||
data: { data: "some data" }, | ||
lastUpdate: new Date(), | ||
}); | ||
|
||
const { container } = render(StaleDataNotice, { target: document.body }); | ||
expect(container.childElementCount).toBe(0); | ||
}); | ||
|
||
it("renders the Stale Data notice if data is stale", async () => { | ||
vi.useFakeTimers(); | ||
vi.setSystemTime(new Date(2024, 4, 20)); | ||
|
||
mockedReadableStore.setMockedStoreValue({ | ||
...initialState, | ||
data: { data: "some data" }, | ||
lastUpdate: new Date(), | ||
}); | ||
|
||
vi.mocked(marketDataStore.isDataStale).mockReturnValueOnce(true); | ||
|
||
const { container } = render(StaleDataNotice, { target: document.body }); | ||
|
||
expect(container.firstChild).toMatchSnapshot(); | ||
|
||
vi.useRealTimers(); | ||
}); | ||
|
||
it("should react to data changes", async () => { | ||
mockedReadableStore.setMockedStoreValue({ | ||
...initialState, | ||
lastUpdate: null, | ||
}); | ||
|
||
const { container } = render(StaleDataNotice, { target: document.body }); | ||
|
||
expect(container.childElementCount).toBe(0); | ||
|
||
vi.mocked(marketDataStore.isDataStale).mockReturnValueOnce(true); | ||
|
||
await act(() => { | ||
mockedReadableStore.setMockedStoreValue({ | ||
...initialState, | ||
lastUpdate: new Date(), | ||
}); | ||
}); | ||
|
||
expect(container.firstChild).toHaveClass("dusk-icon"); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
explorer/src/lib/components/__tests__/__snapshots__/StaleDataNotice.spec.js.snap
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,18 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`StaleDataNotice > renders the Stale Data notice if data is stale 1`] = ` | ||
<svg | ||
class="dusk-icon dusk-icon--size--normal" | ||
data-tooltip-id="main-tooltip" | ||
data-tooltip-place="top" | ||
data-tooltip-text="Stale Data | ||
Last Update: Mon, 20 May 2024 00:00:00 GMT" | ||
data-tooltip-type="warning" | ||
role="graphics-symbol" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
d="M12,2L1,21H23M12,6L19.53,19H4.47M11,10V14H13V10M11,16V18H13V16" | ||
/> | ||
</svg> | ||
`; |
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
19 changes: 19 additions & 0 deletions
19
explorer/src/lib/components/stale-data-notice/StaleDataNotice.svelte
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,19 @@ | ||
<script> | ||
import { mdiAlertOutline } from "@mdi/js"; | ||
import { Icon } from "$lib/dusk/components"; | ||
import { marketDataStore } from "$lib/stores"; | ||
$: tooltipText = `Stale Data \nLast Update: ${$marketDataStore.lastUpdate?.toUTCString()}`; | ||
$: isStaleData = $marketDataStore.lastUpdate && marketDataStore.isDataStale(); | ||
</script> | ||
{#if isStaleData} | ||
<Icon | ||
path={mdiAlertOutline} | ||
size="normal" | ||
data-tooltip-id="main-tooltip" | ||
data-tooltip-text={tooltipText} | ||
data-tooltip-place="top" | ||
data-tooltip-type="warning" | ||
/> | ||
{/if} |
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
Oops, something went wrong.