@@ -348,7 +348,7 @@ abstract class TestRunner {
348
348
349
349
constructor (
350
350
private readonly name : string ,
351
- protected readonly platform : MockPlatform ,
351
+ protected readonly platform : TestPlatform ,
352
352
config : SpecConfig
353
353
) {
354
354
this . databaseInfo = new DatabaseInfo (
@@ -812,7 +812,7 @@ abstract class TestRunner {
812
812
813
813
private doApplyClientState ( state : SpecClientState ) : Promise < void > {
814
814
if ( state . visibility ) {
815
- this . platform . fireVisibilityEvent ( state . visibility ! ) ;
815
+ this . platform . raiseVisibilityEvent ( state . visibility ! ) ;
816
816
}
817
817
return Promise . resolve ( ) ;
818
818
}
@@ -1033,49 +1033,53 @@ class MemoryTestRunner extends TestRunner {
1033
1033
}
1034
1034
1035
1035
/**
1036
- * Implementation of `Platform` that allows mocking of `visibilitychange`
1037
- * events.
1038
- * */
1039
- class MockPlatform implements Platform {
1040
- private visibilityState : VisibilityState = 'unloaded' ;
1041
- private visibilityListener : EventListener | null = null ;
1042
- private mockDocument : Document ;
1043
-
1044
- constructor ( private readonly basePlatform : Platform ) {
1045
- this . initMockDocument ( ) ;
1046
- }
1047
-
1048
- initMockDocument ( ) {
1049
- this . mockDocument = {
1050
- visibilityState : this . visibilityState ,
1051
- addEventListener : ( type : string , listener : EventListener ) => {
1052
- assert (
1053
- type === 'visibilitychange' ,
1054
- "MockPlatform only supports events of type 'visibilitychange'"
1055
- ) ;
1056
- this . visibilityListener = listener ;
1057
- } ,
1058
- removeEventListener : ( type : string , listener : EventListener ) => {
1059
- if ( listener === this . visibilityListener ) {
1060
- this . visibilityListener = null ;
1061
- }
1062
- }
1063
- } as any ; // tslint:disable-line:no-any Not implementing the entire document interface
1036
+ * `Document` mock that implements the `visibilitychange` API used by Firestore.
1037
+ */
1038
+ class MockDocument {
1039
+ private _visibilityState : VisibilityState = 'unloaded' ;
1040
+ private visibilityListener : EventListener | null ;
1041
+
1042
+ get visibilityState ( ) : VisibilityState {
1043
+ return this . _visibilityState ;
1064
1044
}
1065
1045
1066
- fireVisibilityEvent ( visibility : VisibilityState ) {
1067
- this . visibilityState = visibility ;
1046
+ addEventListener ( type : string , listener : EventListener ) {
1047
+ assert (
1048
+ type === 'visibilitychange' ,
1049
+ "MockDocument only supports events of type 'visibilitychange'"
1050
+ ) ;
1051
+ this . visibilityListener = listener ;
1052
+ }
1053
+
1054
+ removeEventListener ( type : string , listener : EventListener ) {
1055
+ if ( listener === this . visibilityListener ) {
1056
+ this . visibilityListener = null ;
1057
+ }
1058
+ }
1059
+
1060
+ raiseVisibilityEvent ( visibility : VisibilityState ) {
1061
+ this . _visibilityState = visibility ;
1068
1062
if ( this . visibilityListener ) {
1069
1063
this . visibilityListener ( new Event ( 'visibilitychange' ) ) ;
1070
1064
}
1071
1065
}
1066
+ }
1067
+
1068
+ /**
1069
+ * Implementation of `Platform` that allows mocking of `document` and `window`.
1070
+ */
1071
+ class TestPlatform implements Platform {
1072
+ private mockDocument = new MockDocument ( ) ;
1073
+
1074
+ constructor ( private readonly basePlatform : Platform ) { }
1072
1075
1073
1076
get window ( ) : Window | null {
1074
1077
return this . basePlatform . window ;
1075
1078
}
1076
1079
1077
1080
get document ( ) : Document {
1078
- return this . mockDocument ;
1081
+ // tslint:disable-next-line:no-any MockDocument doesn't support full Document interface.
1082
+ return ( this . mockDocument as any ) as Document ;
1079
1083
}
1080
1084
1081
1085
get base64Available ( ) : boolean {
@@ -1086,6 +1090,10 @@ class MockPlatform implements Platform {
1086
1090
return this . basePlatform . emptyByteString ;
1087
1091
}
1088
1092
1093
+ raiseVisibilityEvent ( visibility : VisibilityState ) {
1094
+ this . mockDocument . raiseVisibilityEvent ( visibility ) ;
1095
+ }
1096
+
1089
1097
loadConnection ( databaseInfo : DatabaseInfo ) : Promise < Connection > {
1090
1098
return this . basePlatform . loadConnection ( databaseInfo ) ;
1091
1099
}
@@ -1140,7 +1148,7 @@ export async function runSpec(
1140
1148
steps : SpecStep [ ]
1141
1149
) : Promise < void > {
1142
1150
console . log ( 'Running spec: ' + name ) ;
1143
- const platform = new MockPlatform ( PlatformSupport . getPlatform ( ) ) ;
1151
+ const platform = new TestPlatform ( PlatformSupport . getPlatform ( ) ) ;
1144
1152
let runners : TestRunner [ ] = [ ] ;
1145
1153
for ( let i = 0 ; i < config . numClients ; ++ i ) {
1146
1154
if ( usePersistence ) {
0 commit comments