11import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
2- import { ref } from "vue" ;
32import fc from "fast-check" ;
43
4+ import type {
5+ CtrlStateQuery ,
6+ CtrlStateSSubscription ,
7+ } from "@/graphql/codegen/generated" ;
58import {
69 useCtrlStateQuery ,
710 useCtrlStateSSubscription ,
811} from "@/graphql/codegen/generated" ;
9- import type { CtrlStateSSubscription } from "@/graphql/codegen/generated" ;
10- import { onReady } from "@/utils/on-ready" ;
1112
1213import { useSubscribeState } from "../use-state-subscription" ;
1314
15+ import { mockUseQueryResponse } from "./mock-use-query-response" ;
16+ import { mockUseSubscriptionResponse } from "./mock-use-subscription-response" ;
17+
1418vi . mock ( "@/graphql/codegen/generated" , ( ) => ( {
1519 useCtrlStateQuery : vi . fn ( ) ,
1620 useCtrlStateSSubscription : vi . fn ( ) ,
1721} ) ) ;
1822
19- const fcState = fc . oneof ( fc . constant ( undefined ) , fc . string ( { minLength : 1 } ) ) ;
23+ const fcState = fc . string ( { minLength : 1 } ) ;
24+ const fcQueryData = fc . oneof (
25+ fc . constant ( undefined ) ,
26+ fc . record ( { ctrl : fc . record ( { state : fcState } ) } ) ,
27+ ) ;
28+ const fcSubData = fc . oneof ( fc . constant ( undefined ) , fc . record ( { ctrlState : fcState } ) ) ;
2029
2130const fcErrorInstance = fc . string ( ) . map ( ( msg ) => new Error ( msg ) ) ;
2231const fcError = fc . oneof ( fc . constant ( undefined ) , fcErrorInstance ) ;
2332
24- type Query = ReturnType < typeof useCtrlStateQuery > ;
25- type Sub = ReturnType < typeof useCtrlStateSSubscription > ;
26-
27- function createMockQuery (
28- state_value : string | undefined ,
29- error_value : Error | undefined ,
30- ) : Query {
31- type Data = NonNullable < Query [ "data" ] [ "value" ] > ;
32- const data = ref < Data | undefined > ( undefined ) ;
33- const error = ref < Error | undefined > ( undefined ) ;
34-
35- const ready = ( async ( ) => {
36- await Promise . resolve ( ) ;
37- data . value = { ctrl : { state : state_value } } as Data ;
38- error . value = error_value ;
39- } ) ( ) ;
40-
41- return onReady ( { data, error } , ready ) as Query ;
42- }
43-
44- function createMockSubscription (
45- state_value : string | undefined ,
46- error_value : Error | undefined ,
47- ) : Sub {
48- const data = ref < CtrlStateSSubscription | undefined > ( undefined ) ;
49- const error = ref < Error | undefined > ( undefined ) ;
50-
51- const ready = ( async ( ) => {
52- await Promise . resolve ( ) ;
53- data . value = { ctrlState : state_value } as CtrlStateSSubscription ;
54- error . value = error_value ;
55- } ) ( ) ;
56-
57- return onReady ( { data, error } , ready ) as unknown as Sub ;
58- }
33+ const fcQueryArg = fc . record ( { data : fcQueryData , error : fcError } ) ;
34+ const fcSubArg = fc . record ( { data : fcSubData , error : fcError } ) ;
5935
6036describe ( "useSubscribeState()" , ( ) => {
6137 beforeEach ( ( ) => {
@@ -68,26 +44,36 @@ describe("useSubscribeState()", () => {
6844
6945 it ( "Property test" , async ( ) => {
7046 await fc . assert (
71- fc . asyncProperty (
72- fcState ,
73- fcError ,
74- fcState ,
75- fcError ,
76- async ( queryState , queryError , subState , subError ) => {
77- const query = createMockQuery ( queryState , queryError ) ;
78- const sub = createMockSubscription ( subState , subError ) ;
79- vi . mocked ( useCtrlStateQuery ) . mockReturnValue ( query ) ;
80- vi . mocked ( useCtrlStateSSubscription ) . mockReturnValue ( sub ) ;
81- const { state, error } = await useSubscribeState ( ) ;
82- const expectedError = subError || queryError ;
47+ fc . asyncProperty ( fcQueryArg , fc . array ( fcSubArg ) , async ( queryArg , subArg ) => {
48+ // Mock useCtrlStateQuery()
49+ const queryRes = mockUseQueryResponse < CtrlStateQuery > ( queryArg ) ;
50+ type Query = ReturnType < typeof useCtrlStateQuery > ;
51+ vi . mocked ( useCtrlStateQuery ) . mockReturnValue ( queryRes as Query ) ;
52+
53+ // Mock useCtrlStateSSubscription()
54+ const { response : subRes , issue } =
55+ mockUseSubscriptionResponse < CtrlStateSSubscription > ( subArg ) ;
56+ type SubRes = ReturnType < typeof useCtrlStateSSubscription > ;
57+ vi . mocked ( useCtrlStateSSubscription ) . mockReturnValue ( subRes as SubRes ) ;
58+
59+ const { state, error } = await useSubscribeState ( ) ;
60+
61+ // Assert initial values are from query.
62+ expect ( error . value ) . toBe ( queryArg . error ) ;
63+ expect ( state . value ) . toBe (
64+ queryArg . error ? undefined : queryArg . data ?. ctrl . state ,
65+ ) ;
66+
67+ // Assert the subsequent values are issued from subscription backed up by query.
68+ for ( const issued of issue ) {
69+ const expectedError = issued . error || queryArg . error ;
8370 const expectedState = expectedError
8471 ? undefined
85- : subState || queryState || undefined ;
86-
72+ : issued . data ?. ctrlState || queryArg . data ?. ctrl . state ;
8773 expect ( error . value ) . toBe ( expectedError ) ;
8874 expect ( state . value ) . toBe ( expectedState ) ;
89- } ,
90- ) ,
75+ }
76+ } ) ,
9177 ) ;
9278 } ) ;
9379} ) ;
0 commit comments