11import { describe , expect , it , vi } from 'vitest' ;
22
3- import { Maybe } from '../utils/types' ;
3+ import { AnyObject , EmptyObject , Maybe } from '../utils/types' ;
44
55import { AbstractViewModel } from './abstract-view-model' ;
6+ import { AbstractViewModelParams } from './abstract-view-model.types' ;
67import { ViewModel } from './view-model' ;
78
8- export class TestViewModelImpl extends AbstractViewModel < any , any > {
9+ export class TestViewModelImpl <
10+ Payload extends AnyObject = EmptyObject ,
11+ ParentViewModel extends ViewModel < any > | null = null ,
12+ > extends AbstractViewModel < Payload , ParentViewModel > {
913 spies = {
1014 mount : vi . fn ( ) ,
1115 unmount : vi . fn ( ) ,
@@ -14,10 +18,18 @@ export class TestViewModelImpl extends AbstractViewModel<any, any> {
1418 didUnmount : vi . fn ( ) ,
1519 } ;
1620
21+ constructor ( params ?: Partial < AbstractViewModelParams < Payload > > ) {
22+ super ( {
23+ ...params ,
24+ id : params ?. id ?? '1' ,
25+ payload : params ?. payload ?? ( { } as any ) ,
26+ } ) ;
27+ }
28+
1729 protected getParentViewModel (
1830 // eslint-disable-next-line @typescript-eslint/no-unused-vars
1931 parentViewModelId : Maybe < string > ,
20- ) : ViewModel < any , ViewModel < any , any > > | null {
32+ ) : ParentViewModel {
2133 throw new Error ( 'Method not implemented.' ) ;
2234 }
2335
@@ -43,94 +55,83 @@ export class TestViewModelImpl extends AbstractViewModel<any, any> {
4355 this . spies . didUnmount ( ) ;
4456 }
4557}
46- export const createTestViewModel = ( {
47- id = '1' ,
48- payload = { } ,
49- } : {
50- id ?: string ;
51- payload ?: any ;
52- } = { } ) => {
53- const vm = new TestViewModelImpl ( { id, payload } ) ;
54-
55- return vm ;
56- } ;
5758
5859describe ( 'AbstractViewModel' , ( ) => {
5960 it ( 'create instance' , ( ) => {
60- const vm = new TestViewModelImpl ( { id : '1' , payload : { } } ) ;
61+ const vm = new TestViewModelImpl ( ) ;
6162 expect ( vm ) . toBeDefined ( ) ;
6263 } ) ;
6364
6465 it ( 'has id' , ( ) => {
65- const vm = new TestViewModelImpl ( { id : '1' , payload : { } } ) ;
66+ const vm = new TestViewModelImpl ( ) ;
6667 expect ( vm . id ) . toBe ( '1' ) ;
6768 } ) ;
6869
6970 it ( 'has payload' , ( ) => {
70- const vm = new TestViewModelImpl ( { id : '1' , payload : { test : 1 } } ) ;
71+ const vm = new TestViewModelImpl ( { payload : { test : 1 } } ) ;
7172 expect ( vm . payload ) . toEqual ( { test : 1 } ) ;
7273 } ) ;
7374
7475 it ( 'has isMounted' , ( ) => {
75- const vm = new TestViewModelImpl ( { id : '1' , payload : { } } ) ;
76+ const vm = new TestViewModelImpl ( ) ;
7677 expect ( vm . isMounted ) . toBe ( false ) ;
7778 } ) ;
7879
7980 it ( 'has mount method' , ( ) => {
80- const vm = new TestViewModelImpl ( { id : '1' , payload : { } } ) ;
81+ const vm = new TestViewModelImpl ( ) ;
8182 expect ( vm . mount ) . toBeDefined ( ) ;
8283 } ) ;
8384
8485 it ( 'has unmount method' , ( ) => {
85- const vm = new TestViewModelImpl ( { id : '1' , payload : { } } ) ;
86+ const vm = new TestViewModelImpl ( ) ;
8687 expect ( vm . unmount ) . toBeDefined ( ) ;
8788 } ) ;
8889
8990 it ( 'has dispose' , ( ) => {
90- const vm = new TestViewModelImpl ( { id : '1' , payload : { } } ) ;
91+ const vm = new TestViewModelImpl ( ) ;
9192 expect ( vm . dispose ) . toBeDefined ( ) ;
9293 } ) ;
9394
9495 it ( 'mount should be called once' , ( ) => {
95- const vm = new TestViewModelImpl ( { id : '1' , payload : { } } ) ;
96+ const vm = new TestViewModelImpl ( ) ;
9697
9798 vm . mount ( ) ;
9899
99100 expect ( vm . spies . mount ) . toHaveBeenCalledOnce ( ) ;
100101 } ) ;
101102
102103 it ( 'didMount should be called after mount' , ( ) => {
103- const vm = new TestViewModelImpl ( { id : '1' , payload : { } } ) ;
104+ const vm = new TestViewModelImpl ( ) ;
104105
105106 vm . mount ( ) ;
106107
107108 expect ( vm . spies . didMount ) . toHaveBeenCalledOnce ( ) ;
108109 } ) ;
109110
110111 it ( 'isMounted should be true after mount' , ( ) => {
111- const vm = createTestViewModel ( ) ;
112+ const vm = new TestViewModelImpl ( ) ;
112113 vm . mount ( ) ;
113114 expect ( vm . isMounted ) . toBe ( true ) ;
114115 } ) ;
115116
116117 it ( 'unmount should be called once' , ( ) => {
117- const vm = createTestViewModel ( ) ;
118+ const vm = new TestViewModelImpl ( ) ;
118119
119120 vm . unmount ( ) ;
120121
121122 expect ( vm . spies . unmount ) . toHaveBeenCalledOnce ( ) ;
122123 } ) ;
123124
124125 it ( 'didUnmount should be called after unmount' , ( ) => {
125- const vm = createTestViewModel ( ) ;
126+ const vm = new TestViewModelImpl ( ) ;
126127
127128 vm . unmount ( ) ;
128129
129130 expect ( vm . spies . didUnmount ) . toHaveBeenCalledOnce ( ) ;
130131 } ) ;
131132
132133 it ( 'isMounted should be false after unmount' , ( ) => {
133- const vm = createTestViewModel ( ) ;
134+ const vm = new TestViewModelImpl ( ) ;
134135 vm . mount ( ) ;
135136 vm . unmount ( ) ;
136137 expect ( vm . isMounted ) . toBe ( false ) ;
0 commit comments