11import { describe , it , expect } from "vitest" ;
2- import { ref , nextTick } from "vue" ;
2+ import { ref , nextTick , unref , isRef } from "vue" ;
3+
4+ import fc from "fast-check" ;
35
46import { useModel } from ".." ;
57
6- describe ( "useModel()" , ( ) => {
7- it ( "call without arguments" , async ( ) => {
8- const { model, source } = await useModel ( ) ;
9- expect ( source . value ) . toBe ( "" ) ;
10- expect ( model . value ?. getValue ( ) ) . toBe ( "" ) ;
8+ const fcText = ( ) =>
9+ fc . array ( fc . oneof ( fc . lorem ( ) , fc . constant ( "" ) ) , {
10+ maxLength : 100 ,
11+ size : "max" ,
1112 } ) ;
1213
13- it ( "call with string source" , async ( ) => {
14- const source = "# Hello, world!" ;
15- const { model } = await useModel ( { source } ) ;
16- expect ( model . value ?. getValue ( ) ) . toBe ( "# Hello, world!" ) ;
14+ export const fcSource = ( ) =>
15+ fc . option (
16+ fc . oneof (
17+ fcText ( ) . map ( ( lines ) => lines . join ( "\n" ) ) ,
18+ fcText ( ) . map ( ( lines ) => ref ( lines . join ( "\n" ) ) )
19+ ) ,
20+ { nil : undefined }
21+ ) ;
22+
23+ export const fcLanguage = ( ) =>
24+ fc . option ( fc . constantFrom ( "python" , "typescript" ) , {
25+ nil : undefined ,
1726 } ) ;
1827
19- it ( "call with ref source" , async ( ) => {
20- const source = ref ( "# Hello, world!" ) ;
21- const { model, source : sourceReturned } = await useModel ( { source } ) ;
22- expect ( sourceReturned === source ) . toBe ( true ) ;
23- expect ( model . value ?. getValue ( ) ) . toBe ( "# Hello, world!" ) ;
28+ export const fcUseModelOptions = ( ) =>
29+ fc . record (
30+ {
31+ source : fcSource ( ) ,
32+ language : fcLanguage ( ) ,
33+ } ,
34+ { withDeletedKeys : true }
35+ ) ;
36+
37+ describe ( "fcUseModelOptions()" , ( ) => {
38+ it ( "Options of useModel() are generated" , ( ) => {
39+ fc . assert (
40+ fc . property ( fcUseModelOptions ( ) , ( options ) => {
41+ expect ( options ) . toBeDefined ( ) ;
42+ return true ;
43+ } )
44+ ) ;
45+ } ) ;
46+ } ) ;
47+
48+ const fcUseModelArgs = ( ) =>
49+ fc . option (
50+ fcUseModelOptions ( ) . map ( ( options ) => [ options ] ) ,
51+ { nil : [ ] }
52+ ) ;
53+
54+ function assertDefined < T > ( value : T ) : asserts value is NonNullable < T > {
55+ expect ( value ) . toBeDefined ( ) ;
56+ }
57+
58+ describe ( "useModel()" , ( ) => {
59+ it ( "Property test" , async ( ) => {
60+ await fc . assert (
61+ fc . asyncProperty ( fcUseModelArgs ( ) , fc . boolean ( ) , async ( args , early ) => {
62+ const { model, source, dispose, ready } = early
63+ ? useModel ( ...args )
64+ : await useModel ( ...args ) ;
65+ if ( early ) await ready ;
66+
67+ const model_ = model . value ;
68+ assertDefined ( model_ ) ;
69+
70+ const options = args ?. [ 0 ] ;
71+
72+ const expectedLanguage = options ?. language ?? "python" ;
73+ expect ( model_ . getLanguageId ( ) ) . toBe ( expectedLanguage ) ;
74+
75+ if ( isRef ( options ?. source ) ) {
76+ expect ( source . value ) . toBe ( options ?. source . value ) ;
77+ }
78+
79+ const expectedValue = unref ( options ?. source ?? "" ) ;
80+ expect ( model_ . getValue ( ) ) . toBe ( expectedValue ) ;
81+
82+ dispose ( ) ;
83+ } ) ,
84+ { verbose : true , numRuns : 10 } // NOTE: Error with larger numRuns
85+ ) ;
2486 } ) ;
2587
26- it ( "source is reactive" , async ( ) => {
88+ it ( "Source is reactive" , async ( ) => {
2789 const source = ref ( "# Hello, world!" ) ;
2890 const { model } = await useModel ( { source } ) ;
2991 expect ( model . value ?. getValue ( ) ) . toBe ( "# Hello, world!" ) ;
@@ -32,7 +94,7 @@ describe("useModel()", () => {
3294 expect ( model . value ?. getValue ( ) ) . toBe ( "# New source" ) ;
3395 } ) ;
3496
35- it ( "edit model" , async ( ) => {
97+ it ( "Edit model" , async ( ) => {
3698 const source = ref ( "# Hello, world!" ) ;
3799 const { model } = await useModel ( { source } ) ;
38100 expect ( model . value ?. getValue ( ) ) . toBe ( "# Hello, world!" ) ;
0 commit comments