@@ -50,6 +50,7 @@ import {
5050 typeInField ,
5151 uploadFile ,
5252 selectFromSelector ,
53+ waitForUpdate ,
5354} from './interface' ;
5455import type { RunSagaFunction } from './store' ;
5556import { createStore , resolveInterface , setInterface } from './store' ;
@@ -90,6 +91,7 @@ describe('getInterfaceResponse', () => {
9091 selectFromRadioGroup : jest . fn ( ) ,
9192 selectFromSelector : jest . fn ( ) ,
9293 uploadFile : jest . fn ( ) ,
94+ waitForUpdate : jest . fn ( ) ,
9395 } ;
9496
9597 it ( 'returns an `ok` function that resolves the user interface with `null` for alert dialogs' , async ( ) => {
@@ -111,6 +113,7 @@ describe('getInterfaceResponse', () => {
111113 selectFromRadioGroup : expect . any ( Function ) ,
112114 selectFromSelector : expect . any ( Function ) ,
113115 uploadFile : expect . any ( Function ) ,
116+ waitForUpdate : expect . any ( Function ) ,
114117 ok : expect . any ( Function ) ,
115118 } ) ;
116119
@@ -138,6 +141,7 @@ describe('getInterfaceResponse', () => {
138141 selectFromRadioGroup : expect . any ( Function ) ,
139142 selectFromSelector : expect . any ( Function ) ,
140143 uploadFile : expect . any ( Function ) ,
144+ waitForUpdate : expect . any ( Function ) ,
141145 ok : expect . any ( Function ) ,
142146 cancel : expect . any ( Function ) ,
143147 } ) ;
@@ -166,6 +170,7 @@ describe('getInterfaceResponse', () => {
166170 selectFromRadioGroup : expect . any ( Function ) ,
167171 selectFromSelector : expect . any ( Function ) ,
168172 uploadFile : expect . any ( Function ) ,
173+ waitForUpdate : expect . any ( Function ) ,
169174 ok : expect . any ( Function ) ,
170175 cancel : expect . any ( Function ) ,
171176 } ) ;
@@ -194,6 +199,7 @@ describe('getInterfaceResponse', () => {
194199 selectFromRadioGroup : expect . any ( Function ) ,
195200 selectFromSelector : expect . any ( Function ) ,
196201 uploadFile : expect . any ( Function ) ,
202+ waitForUpdate : expect . any ( Function ) ,
197203 ok : expect . any ( Function ) ,
198204 cancel : expect . any ( Function ) ,
199205 } ) ;
@@ -222,6 +228,7 @@ describe('getInterfaceResponse', () => {
222228 selectFromRadioGroup : expect . any ( Function ) ,
223229 selectFromSelector : expect . any ( Function ) ,
224230 uploadFile : expect . any ( Function ) ,
231+ waitForUpdate : expect . any ( Function ) ,
225232 ok : expect . any ( Function ) ,
226233 cancel : expect . any ( Function ) ,
227234 } ) ;
@@ -250,6 +257,7 @@ describe('getInterfaceResponse', () => {
250257 selectFromRadioGroup : expect . any ( Function ) ,
251258 selectFromSelector : expect . any ( Function ) ,
252259 uploadFile : expect . any ( Function ) ,
260+ waitForUpdate : expect . any ( Function ) ,
253261 ok : expect . any ( Function ) ,
254262 cancel : expect . any ( Function ) ,
255263 } ) ;
@@ -296,6 +304,7 @@ describe('getInterfaceResponse', () => {
296304 selectInDropdown : expect . any ( Function ) ,
297305 selectFromRadioGroup : expect . any ( Function ) ,
298306 selectFromSelector : expect . any ( Function ) ,
307+ waitForUpdate : expect . any ( Function ) ,
299308 uploadFile : expect . any ( Function ) ,
300309 } ) ;
301310 } ) ;
@@ -336,6 +345,7 @@ describe('getInterfaceResponse', () => {
336345 selectFromRadioGroup : expect . any ( Function ) ,
337346 selectFromSelector : expect . any ( Function ) ,
338347 uploadFile : expect . any ( Function ) ,
348+ waitForUpdate : expect . any ( Function ) ,
339349 cancel : expect . any ( Function ) ,
340350 } ) ;
341351 } ) ;
@@ -370,6 +380,7 @@ describe('getInterfaceResponse', () => {
370380 selectFromRadioGroup : expect . any ( Function ) ,
371381 selectFromSelector : expect . any ( Function ) ,
372382 uploadFile : expect . any ( Function ) ,
383+ waitForUpdate : expect . any ( Function ) ,
373384 cancel : expect . any ( Function ) ,
374385 ok : expect . any ( Function ) ,
375386 } ) ;
@@ -1250,6 +1261,7 @@ describe('getInterface', () => {
12501261 selectFromRadioGroup : expect . any ( Function ) ,
12511262 selectFromSelector : expect . any ( Function ) ,
12521263 uploadFile : expect . any ( Function ) ,
1264+ waitForUpdate : expect . any ( Function ) ,
12531265 ok : expect . any ( Function ) ,
12541266 } ) ;
12551267 } ) ;
@@ -1280,6 +1292,7 @@ describe('getInterface', () => {
12801292 selectFromRadioGroup : expect . any ( Function ) ,
12811293 selectFromSelector : expect . any ( Function ) ,
12821294 uploadFile : expect . any ( Function ) ,
1295+ waitForUpdate : expect . any ( Function ) ,
12831296 ok : expect . any ( Function ) ,
12841297 } ) ;
12851298 } ) ;
@@ -1468,6 +1481,41 @@ describe('getInterface', () => {
14681481 } ,
14691482 ) ;
14701483 } ) ;
1484+
1485+ it ( 'waits for the interface content to update when `waitForUpdate` is called' , async ( ) => {
1486+ jest . spyOn ( rootControllerMessenger , 'call' ) ;
1487+ const { store, runSaga } = createStore ( getMockOptions ( ) ) ;
1488+
1489+ const content = (
1490+ < Box >
1491+ < Input name = "foo" />
1492+ </ Box >
1493+ ) ;
1494+ const id = await interfaceController . createInterface ( MOCK_SNAP_ID , content ) ;
1495+ const type = DialogType . Alert ;
1496+ const ui = { type : DIALOG_APPROVAL_TYPES [ type ] , id } ;
1497+
1498+ store . dispatch ( setInterface ( ui ) ) ;
1499+
1500+ const result = await runSaga (
1501+ getInterface ,
1502+ runSaga ,
1503+ MOCK_SNAP_ID ,
1504+ rootControllerMessenger ,
1505+ ) . toPromise ( ) ;
1506+
1507+ const promise = result . waitForUpdate ( ) ;
1508+
1509+ await interfaceController . updateInterface (
1510+ MOCK_SNAP_ID ,
1511+ id ,
1512+ < Text > Hello world!</ Text > ,
1513+ ) ;
1514+
1515+ const newInterface = await promise ;
1516+
1517+ expect ( newInterface . content . type ) . toBe ( 'Text' ) ;
1518+ } ) ;
14711519} ) ;
14721520
14731521describe ( 'selectFromRadioGroup' , ( ) => {
@@ -1761,3 +1809,40 @@ describe('selectFromSelector', () => {
17611809 ) ;
17621810 } ) ;
17631811} ) ;
1812+
1813+ describe ( 'waitForUpdate' , ( ) => {
1814+ const rootControllerMessenger = getRootControllerMessenger ( ) ;
1815+ const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger (
1816+ rootControllerMessenger ,
1817+ ) ;
1818+
1819+ const interfaceController = new SnapInterfaceController ( {
1820+ messenger : controllerMessenger ,
1821+ } ) ;
1822+
1823+ it ( 'waits for the interface content to update' , async ( ) => {
1824+ const content = < Input name = "foo" /> ;
1825+
1826+ const interfaceId = await interfaceController . createInterface (
1827+ MOCK_SNAP_ID ,
1828+ content ,
1829+ ) ;
1830+
1831+ const promise = waitForUpdate (
1832+ rootControllerMessenger ,
1833+ MOCK_SNAP_ID ,
1834+ interfaceId ,
1835+ content ,
1836+ ) ;
1837+
1838+ await interfaceController . updateInterface (
1839+ MOCK_SNAP_ID ,
1840+ interfaceId ,
1841+ < Text > Hello world!</ Text > ,
1842+ ) ;
1843+
1844+ const newInterface = await promise ;
1845+
1846+ expect ( newInterface . content . type ) . toBe ( 'Text' ) ;
1847+ } ) ;
1848+ } ) ;
0 commit comments