11import log from 'loglevel' ;
22import nock from 'nock' ;
3- import ReadOnlyNetworkStore from './read-only-network-store' ;
3+ import browser from 'webextension-polyfill' ;
4+ import { FixtureExtensionStore } from './fixture-extension-store' ;
45
56const FIXTURE_SERVER_HOST = 'localhost' ;
67const FIXTURE_SERVER_PORT = 12345 ;
@@ -13,15 +14,28 @@ const DEFAULT_INITIAL_STATE = {
1314
1415const MOCK_STATE = { data : { config : { foo : 'bar' } } , meta : { version : 1 } } ;
1516
16- /**
17- * Initiatilizes a ReadOnlyNetworkStore for testing
18- *
19- * @returns store - a ReadOnlyNetworkStore
20- */
21- function setupReadOnlyNetworkStore ( ) {
22- const store = new ReadOnlyNetworkStore ( ) ;
23- return store ;
24- }
17+ jest . mock ( 'webextension-polyfill' , ( ) => {
18+ class MockBrowserStorage {
19+ #state: unknown = null ;
20+
21+ async get ( ) {
22+ return this . #state;
23+ }
24+
25+ async set ( value : unknown ) {
26+ this . #state = value ;
27+ }
28+
29+ async clear ( ) {
30+ this . #state = null ;
31+ }
32+ }
33+
34+ return ( {
35+ runtime : { lastError : null } ,
36+ storage : { local : new MockBrowserStorage ( ) } ,
37+ } )
38+ } ) ;
2539
2640/**
2741 * Create a Nock scope for the fixture server response.
@@ -43,16 +57,16 @@ function setMockFixtureServerReply(
4357 mockFixtureServerInterceptor ( ) . reply ( 200 , state ) ;
4458}
4559
46- describe ( 'ReadOnlyNetworkStore ' , ( ) => {
47- beforeEach ( ( ) => {
48- jest . resetModules ( ) ;
60+ describe ( 'FixtureExtensionStore ' , ( ) => {
61+ beforeEach ( async ( ) => {
62+ await browser . storage . local . clear ( ) ;
4963 nock . cleanAll ( ) ;
5064 } ) ;
5165
5266 describe ( 'constructor' , ( ) => {
5367 it ( 'loads state from the network if fetch is successful and response is ok' , async ( ) => {
5468 setMockFixtureServerReply ( MOCK_STATE ) ;
55- const store = setupReadOnlyNetworkStore ( ) ;
69+ const store = new FixtureExtensionStore ( ) ;
5670
5771 const result = await store . get ( ) ;
5872
@@ -64,7 +78,7 @@ describe('ReadOnlyNetworkStore', () => {
6478 . spyOn ( log , 'debug' )
6579 . mockImplementation ( ( ) => undefined ) ;
6680 mockFixtureServerInterceptor ( ) . reply ( 400 ) ;
67- const store = setupReadOnlyNetworkStore ( ) ;
81+ const store = new FixtureExtensionStore ( ) ;
6882
6983 const result = await store . get ( ) ;
7084
@@ -79,7 +93,7 @@ describe('ReadOnlyNetworkStore', () => {
7993 const logDebugSpy = jest
8094 . spyOn ( log , 'debug' )
8195 . mockImplementation ( ( ) => undefined ) ;
82- const store = setupReadOnlyNetworkStore ( ) ;
96+ const store = new FixtureExtensionStore ( ) ;
8397
8498 const result = await store . get ( ) ;
8599
@@ -91,18 +105,9 @@ describe('ReadOnlyNetworkStore', () => {
91105 } ) ;
92106
93107 describe ( 'get' , ( ) => {
94- it ( 'returns null if #state is null' , async ( ) => {
95- mockFixtureServerInterceptor ( ) . reply ( 200 ) ;
96- const store = setupReadOnlyNetworkStore ( ) ;
97-
98- const result = await store . get ( ) ;
99-
100- expect ( result ) . toBe ( null ) ;
101- } ) ;
102-
103- it ( 'returns null if state is null' , async ( ) => {
108+ it ( 'returns fixture state after waiting for init' , async ( ) => {
104109 setMockFixtureServerReply ( MOCK_STATE ) ;
105- const store = setupReadOnlyNetworkStore ( ) ;
110+ const store = new FixtureExtensionStore ( ) ;
106111
107112 const result = await store . get ( ) ;
108113
@@ -111,17 +116,8 @@ describe('ReadOnlyNetworkStore', () => {
111116 } ) ;
112117
113118 describe ( 'set' , ( ) => {
114- it ( 'throws if not passed a state parameter' , async ( ) => {
115- const store = setupReadOnlyNetworkStore ( ) ;
116-
117- await expect (
118- // @ts -expect-error Intentionally passing incorrect type
119- store . set ( undefined ) ,
120- ) . rejects . toThrow ( 'MetaMask - updated state is missing' ) ;
121- } ) ;
122-
123119 it ( 'sets the state' , async ( ) => {
124- const store = setupReadOnlyNetworkStore ( ) ;
120+ const store = new FixtureExtensionStore ( ) ;
125121
126122 await store . set ( {
127123 data : { appState : { test : true } } ,
0 commit comments