|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | | -import { ref, nextTick } from "vue"; |
| 2 | +import { ref, nextTick, unref, isRef } from "vue"; |
| 3 | +import type { Ref } from "vue"; |
| 4 | + |
| 5 | +import fc from "fast-check"; |
3 | 6 |
|
4 | 7 | import { withAsyncSetup } from "@/tests/test-utils"; |
5 | 8 | import { useMonacoEditor } from ".."; |
6 | 9 |
|
7 | | -describe("useMonacoEditor()", () => { |
8 | | - it("Editor is created", async () => { |
9 | | - let result!: Awaited<ReturnType<typeof useMonacoEditor>>; |
10 | | - const element = ref(document.createElement("div")); |
11 | | - const source = ref("# Hello, world!"); |
12 | | - await withAsyncSetup(async () => { |
13 | | - result = await useMonacoEditor({ element, source }); |
14 | | - }); |
15 | | - const { editor, model } = result; |
16 | | - expect(editor.value).toBeDefined(); |
17 | | - expect(model.value?.getValue()).toBe("# Hello, world!"); |
| 10 | +import { fcSource, fcLanguage } from "./model.spec"; |
| 11 | + |
| 12 | +const fcElement = () => |
| 13 | + fc |
| 14 | + .oneof( |
| 15 | + fc.integer().map(() => document.createElement("div")), |
| 16 | + fc.constant(undefined) |
| 17 | + ) |
| 18 | + .chain( |
| 19 | + (element): fc.Arbitrary<HTMLElement | Ref<HTMLElement | undefined>> => |
| 20 | + element === undefined |
| 21 | + ? fc.constant(ref(element)) |
| 22 | + : fc.oneof(fc.constant(element), fc.constant(ref(element))) |
| 23 | + ); |
| 24 | + |
| 25 | +const fcMode = () => |
| 26 | + fc.option(fc.constantFrom("viewer" as const, "editor" as const), { nil: undefined }); |
| 27 | + |
| 28 | +const fcUseMonacoEditorOptions = () => |
| 29 | + fc.record( |
| 30 | + { |
| 31 | + element: fcElement(), |
| 32 | + mode: fcMode(), |
| 33 | + source: fcSource(), |
| 34 | + language: fcLanguage(), |
| 35 | + }, |
| 36 | + { requiredKeys: ["element"] } |
| 37 | + ); |
| 38 | + |
| 39 | +describe("fcUseMonacoEditorOptions()", () => { |
| 40 | + it("Options of useMonacoEditor() are generated", () => { |
| 41 | + fc.assert( |
| 42 | + fc.property(fcUseMonacoEditorOptions(), (options) => { |
| 43 | + expect(options).toBeDefined(); |
| 44 | + return true; |
| 45 | + }) |
| 46 | + ); |
18 | 47 | }); |
| 48 | +}); |
19 | 49 |
|
20 | | - it("The 'ready' is awaited", async () => { |
21 | | - let result!: ReturnType<typeof useMonacoEditor>; |
22 | | - const element = ref(document.createElement("div")); |
23 | | - const source = ref("# Hello, world!"); |
24 | | - await withAsyncSetup(async () => { |
25 | | - result = useMonacoEditor({ element, source }); |
26 | | - }); |
27 | | - const { editor, model, ready } = result; |
28 | | - await ready; |
29 | | - expect(editor.value).toBeDefined(); |
30 | | - expect(model.value?.getValue()).toBe("# Hello, world!"); |
| 50 | +describe("useMonacoEditor()", () => { |
| 51 | + it("Property test", async () => { |
| 52 | + await fc.assert( |
| 53 | + fc.asyncProperty( |
| 54 | + fcUseMonacoEditorOptions(), |
| 55 | + fc.boolean(), |
| 56 | + async (options, early) => { |
| 57 | + let editor!: ReturnType<typeof useMonacoEditor>["editor"]; |
| 58 | + let model!: ReturnType<typeof useMonacoEditor>["model"]; |
| 59 | + let source!: ReturnType<typeof useMonacoEditor>["source"]; |
| 60 | + let mode!: ReturnType<typeof useMonacoEditor>["mode"]; |
| 61 | + let ready!: ReturnType<typeof useMonacoEditor>["ready"]; |
| 62 | + const { wrapper } = await withAsyncSetup(async () => { |
| 63 | + ({ editor, model, source, mode, ready } = early |
| 64 | + ? useMonacoEditor(options) |
| 65 | + : await useMonacoEditor(options)); |
| 66 | + }); |
| 67 | + if (early) await ready; |
| 68 | + if (isRef(options.element) && options.element.value === undefined) { |
| 69 | + options.element.value = document.createElement("div"); |
| 70 | + } |
| 71 | + await nextTick(); |
| 72 | + expect(editor.value).toBeDefined(); |
| 73 | + |
| 74 | + const expectedSource = unref(options.source) ?? ""; |
| 75 | + expect(source.value).toBe(expectedSource); |
| 76 | + expect(model.value?.getValue()).toBe(expectedSource); |
| 77 | + |
| 78 | + const expectedMode = unref(options.mode) ?? "viewer"; |
| 79 | + expect(mode.value).toBe(expectedMode); |
| 80 | + |
| 81 | + wrapper.unmount(); |
| 82 | + } |
| 83 | + ) |
| 84 | + ); |
31 | 85 | }); |
32 | 86 |
|
33 | 87 | it("The mode changes between 'viewer' and 'editor'", async () => { |
|
0 commit comments