-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
93 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { beforeEach, describe, expect, test, vi } from "vitest" | ||
import { nextTick, ref } from "vue" | ||
|
||
describe("useTargetLawXml", () => { | ||
beforeEach(() => { | ||
vi.resetModules() | ||
vi.resetAllMocks() | ||
}) | ||
|
||
test("should provide the target law xml", async () => { | ||
const getTargetLawXmlByEli = vi.fn().mockResolvedValue("<xml></xml>") | ||
|
||
vi.doMock("@/services/targetLawsService", () => ({ | ||
getTargetLawXmlByEli, | ||
})) | ||
|
||
const { useTargetLawXml } = await import("./useTargetLawXml") | ||
|
||
const eli = ref( | ||
"eli/bund/bgbl-1/1990/s2954/2022-12-19/1/deu/regelungstext-1", | ||
) | ||
const xml = useTargetLawXml(eli) | ||
// wait for the getTargetLawXmlByEli promise to be resolved | ||
await vi.waitUntil(() => xml.value, { | ||
timeout: 100, | ||
interval: 20, | ||
}) | ||
|
||
expect(xml.value).toBe("<xml></xml>") | ||
}) | ||
|
||
test("should load the target law xml when the eli changes", async () => { | ||
const getTargetLawXmlByEli = vi.fn() | ||
|
||
vi.doMock("@/services/targetLawsService", () => ({ | ||
getTargetLawXmlByEli, | ||
})) | ||
|
||
const { useTargetLawXml } = await import("./useTargetLawXml") | ||
|
||
const eli = ref( | ||
"eli/bund/bgbl-1/1990/s2954/2022-12-19/1/deu/regelungstext-1", | ||
) | ||
useTargetLawXml(eli) | ||
|
||
eli.value = "eli/bund/bgbl-1/1964/s593/1964-08-05/1/deu/regelungstext-1" | ||
await nextTick() | ||
|
||
eli.value = "eli/bund/bgbl-1/1964/s593/1964-08-05/1/deu/regelungstext-1" | ||
await nextTick() | ||
|
||
expect(getTargetLawXmlByEli).toBeCalledTimes(2) | ||
}) | ||
}) |
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,32 @@ | ||
import { | ||
readonly, | ||
watch, | ||
MaybeRefOrGetter, | ||
toValue, | ||
Ref, | ||
DeepReadonly, | ||
ref, | ||
} from "vue" | ||
import { getTargetLawXmlByEli } from "@/services/targetLawsService" | ||
|
||
/** | ||
* Get the xml of a target law. | ||
* @param eli a reference to the eli for which the law xml will be returned. Changing the value of the reference will load the data for the new eli. | ||
* @returns A reference to the target law xml or undefined if it is not available (or still loading). | ||
*/ | ||
export function useTargetLawXml( | ||
eli: MaybeRefOrGetter<string>, | ||
): DeepReadonly<Ref<string | undefined>> { | ||
const targetLawXml = ref<string>() | ||
watch( | ||
() => toValue(eli), | ||
async (eli) => { | ||
if (eli) { | ||
targetLawXml.value = await getTargetLawXmlByEli(eli) | ||
} | ||
}, | ||
{ immediate: true }, | ||
) | ||
|
||
return readonly(targetLawXml) | ||
} |
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