11import { renderWithLayout } from "@test/unit/renderWithLayout" ;
22import { screen } from "@testing-library/react" ;
33import * as effectorReact from "effector-react" ;
4- import { describe , it , vi } from "vitest" ;
4+ import { describe , it } from "vitest" ;
55import { measure } from "../../shared/utils/measure" ;
66import { AccountInfo } from "./AccountInfo" ;
7-
8- // biome-ignore lint/suspicious/noExplicitAny: <explanation>
9- const getUseUnitReturnedValue = ( args : any = { } ) : any => {
10- return [
11- args . account ?? { address : "0x123" , balance : "1000" } ,
12- args . accountCometaInfo ?? null ,
13- args . isLoading ?? false ,
14- args . isLoadingCometaInfo ?? false ,
15- args . params ?? { address : "0x123" } ,
16- args . cometa ?? null ,
17- ] ;
7+ import { fork , type Scope , type TypeOfSource } from "effector" ;
8+ import {
9+ $account ,
10+ $accountCometaInfo ,
11+ loadAccountCometaInfoFx ,
12+ loadAccountStateFx ,
13+ } from "../model" ;
14+ import { $cometaClient } from "../../cometa/model" ;
15+ import "../init.ts" ;
16+ import { addressRoute } from "../../routing/routes/addressRoute.ts" ;
17+
18+ const initScope = ( args ?: {
19+ account ?: Partial < TypeOfSource < typeof $account > > ;
20+ accountCometaInfo ?: Partial < TypeOfSource < typeof $accountCometaInfo > > ;
21+ isLoading ?: boolean ;
22+ isLoadingCometaInfo ?: boolean ;
23+ params ?: Partial < TypeOfSource < typeof addressRoute . $params > > ;
24+ cometa ?: Partial < TypeOfSource < typeof $cometaClient > > ;
25+ } ) : Scope => {
26+ return fork ( {
27+ values : [
28+ [ $account , args ?. account ?? { address : "0x123" , balance : "1000" } ] ,
29+ [ $accountCometaInfo , args ?. accountCometaInfo ?? null ] ,
30+ [ loadAccountStateFx . pending , args ?. isLoading ?? false ] ,
31+ [ loadAccountCometaInfoFx . pending , args ?. isLoadingCometaInfo ?? false ] ,
32+ [ addressRoute . $params , args ?. params ?? { address : "0x123" } ] ,
33+ [ addressRoute . $isOpened , true ] ,
34+ [ $cometaClient , args ?. cometa ?? null ] ,
35+ ] ,
36+ } ) ;
1837} ;
1938
20- vi . mock ( "../../routing" , ( ) => ( {
21- addressRoute : {
22- $params : { getState : ( ) => ( { address : "0x123" } ) } ,
23- $isOpened : { getState : ( ) => true } ,
24- } ,
25- } ) ) ;
26-
27- vi . mock ( "effector-react" ) ;
28- const mockUseUnit = vi . mocked ( effectorReact . useUnit ) ;
29-
3039describe ( "AccountInfo" , ( ) => {
31- afterEach ( ( ) => {
32- vi . clearAllMocks ( ) ;
33- } ) ;
34-
3540 it ( "renders without crashing" , ( ) => {
36- mockUseUnit . mockReturnValue ( getUseUnitReturnedValue ( ) ) ;
37-
38- renderWithLayout ( < AccountInfo /> ) ;
41+ const scope = initScope ( ) ;
42+ renderWithLayout (
43+ < effectorReact . Provider value = { scope } >
44+ < AccountInfo />
45+ </ effectorReact . Provider > ,
46+ ) ;
3947
4048 expect ( screen . getByTestId ( "vitest-unit--account-container" ) ) . toBeInTheDocument ( ) ;
4149 } ) ;
4250
4351 it ( "renders skeleton when loading" , ( ) => {
44- mockUseUnit . mockReturnValue ( getUseUnitReturnedValue ( { account : null , isLoading : true } ) ) ;
45-
46- renderWithLayout ( < AccountInfo /> ) ;
52+ const scope = initScope ( { isLoading : true } ) ;
53+ renderWithLayout (
54+ < effectorReact . Provider value = { scope } >
55+ < AccountInfo />
56+ </ effectorReact . Provider > ,
57+ ) ;
4758
4859 expect ( screen . getByRole ( "progressbar" ) ) . toBeInTheDocument ( ) ;
4960 } ) ;
5061
5162 it ( "renders account information when loaded" , ( ) => {
52- mockUseUnit . mockReturnValue (
53- getUseUnitReturnedValue ( {
54- account : { address : "0x123" , balance : "1000" , tokens : [ ] } ,
55- } ) ,
63+ const scope = initScope ( {
64+ account : { balance : "1000" , tokens : [ ] } ,
65+ } ) ;
66+
67+ renderWithLayout (
68+ < effectorReact . Provider value = { scope } >
69+ < AccountInfo />
70+ </ effectorReact . Provider > ,
5671 ) ;
5772
58- renderWithLayout ( < AccountInfo /> ) ;
59-
6073 expect ( screen . getByText ( "Address" ) ) . toBeInTheDocument ( ) ;
6174 expect ( screen . getByText ( "0x123" ) ) . toBeInTheDocument ( ) ;
6275 expect ( screen . getByText ( "Balance" ) ) . toBeInTheDocument ( ) ;
@@ -67,27 +80,35 @@ describe("AccountInfo", () => {
6780 } ) ;
6881
6982 it ( "renders fallback UI when source code is not available" , ( ) => {
70- mockUseUnit . mockReturnValue (
71- getUseUnitReturnedValue ( {
72- accountCometaInfo : { sourceCode : { Compiled_Contracts : "" } } ,
73- } ) ,
83+ const scope = initScope ( {
84+ accountCometaInfo : {
85+ sourceCode : {
86+ Compiled_Contracts : "" ,
87+ } ,
88+ } ,
89+ } ) ;
90+
91+ renderWithLayout (
92+ < effectorReact . Provider value = { scope } >
93+ < AccountInfo />
94+ </ effectorReact . Provider > ,
7495 ) ;
7596
76- renderWithLayout ( < AccountInfo /> ) ;
77-
7897 expect ( screen . getByText ( "Source code" ) ) . toBeInTheDocument ( ) ;
7998 expect ( screen . getByText ( "Not available" ) ) . toBeInTheDocument ( ) ;
8099 } ) ;
81100
82101 it ( "shows spinner when cometa info is loading" , ( ) => {
83- mockUseUnit . mockReturnValue (
84- getUseUnitReturnedValue ( {
85- isLoadingCometaInfo : true ,
86- } ) ,
102+ const scope = initScope ( {
103+ isLoadingCometaInfo : true ,
104+ } ) ;
105+
106+ renderWithLayout (
107+ < effectorReact . Provider value = { scope } >
108+ < AccountInfo />
109+ </ effectorReact . Provider > ,
87110 ) ;
88111
89- renderWithLayout ( < AccountInfo /> ) ;
90-
91112 expect ( screen . getByTestId ( "vitest-unit--loading-cometa-info-spinner" ) ) . toBeInTheDocument ( ) ;
92113 } ) ;
93114} ) ;
0 commit comments