-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Disable auto reload if page is not visible in ng_tensorboard #3498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
| import {DOCUMENT} from '@angular/common'; | ||
| import {TestBed, fakeAsync, tick} from '@angular/core/testing'; | ||
| import {Store} from '@ngrx/store'; | ||
| import {provideMockStore, MockStore} from '@ngrx/store/testing'; | ||
|
|
@@ -27,10 +28,34 @@ import {createState, createCoreState} from '../core/testing'; | |
| describe('reloader_component', () => { | ||
| let store: MockStore<State>; | ||
| let dispatchSpy: jasmine.Spy; | ||
| let fakeDocument: Document; | ||
|
|
||
| function createFakeDocument() { | ||
| return { | ||
| visibilityState: 'visible', | ||
| addEventListener: document.addEventListener.bind(document), | ||
| removeEventListener: document.removeEventListener.bind(document), | ||
| // DOMTestComponentRenderer injects DOCUMENT and requires the following | ||
| // properties to function. | ||
| querySelectorAll: document.querySelectorAll.bind(document), | ||
| body: document.body, | ||
| }; | ||
| } | ||
|
|
||
| function simulateVisibilityChange(visible: boolean) { | ||
| Object.defineProperty(fakeDocument, 'visibilityState', { | ||
| get: () => (visible ? 'visible' : 'hidden'), | ||
| }); | ||
| document.dispatchEvent(new Event('visibilitychange')); | ||
| } | ||
|
|
||
| beforeEach(async () => { | ||
| await TestBed.configureTestingModule({ | ||
| providers: [ | ||
| { | ||
| provide: DOCUMENT, | ||
| useFactory: createFakeDocument, | ||
| }, | ||
| provideMockStore({ | ||
| initialState: createState( | ||
| createCoreState({ | ||
|
|
@@ -44,6 +69,7 @@ describe('reloader_component', () => { | |
| declarations: [ReloaderComponent], | ||
| }).compileComponents(); | ||
| store = TestBed.get(Store); | ||
| fakeDocument = TestBed.get(DOCUMENT); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just wondering, what happens if you change this to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Acknowledged. |
||
| dispatchSpy = spyOn(store, 'dispatch'); | ||
| }); | ||
|
|
||
|
|
@@ -69,7 +95,8 @@ describe('reloader_component', () => { | |
| expect(dispatchSpy).toHaveBeenCalledTimes(2); | ||
| expect(dispatchSpy).toHaveBeenCalledWith(reload()); | ||
|
|
||
| // // Manually invoke destruction of the component so we can cleanup the timer. | ||
| // Manually invoke destruction of the component so we can cleanup the | ||
| // timer. | ||
| fixture.destroy(); | ||
| })); | ||
|
|
||
|
|
@@ -161,4 +188,158 @@ describe('reloader_component', () => { | |
|
|
||
| fixture.destroy(); | ||
| })); | ||
|
|
||
| it('does not reload if document is not visible', fakeAsync(() => { | ||
| store.setState( | ||
| createState( | ||
| createCoreState({ | ||
| reloadPeriodInMs: 5, | ||
| reloadEnabled: true, | ||
| }) | ||
| ) | ||
| ); | ||
| const fixture = TestBed.createComponent(ReloaderComponent); | ||
| fixture.detectChanges(); | ||
|
|
||
| tick(5); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| simulateVisibilityChange(false); | ||
| tick(5); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| fixture.destroy(); | ||
bmd3k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| })); | ||
|
|
||
| it('reloads when document becomes visible if missed reload', fakeAsync(() => { | ||
| store.setState( | ||
| createState( | ||
| createCoreState({ | ||
| reloadPeriodInMs: 5, | ||
| reloadEnabled: true, | ||
| }) | ||
| ) | ||
| ); | ||
| const fixture = TestBed.createComponent(ReloaderComponent); | ||
| fixture.detectChanges(); | ||
|
|
||
| // Miss a reload because not visible. | ||
| simulateVisibilityChange(false); | ||
| tick(5); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(0); | ||
|
|
||
| // Dispatch a reload when next visible. | ||
| simulateVisibilityChange(true); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| fixture.destroy(); | ||
| })); | ||
|
|
||
| it('reloads when document becomes visible if missed reload, regardless of how long not visible', fakeAsync(() => { | ||
stephanwlee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| store.setState( | ||
| createState( | ||
| createCoreState({ | ||
| reloadPeriodInMs: 5, | ||
| reloadEnabled: true, | ||
| }) | ||
| ) | ||
| ); | ||
| const fixture = TestBed.createComponent(ReloaderComponent); | ||
| fixture.detectChanges(); | ||
|
|
||
| tick(5); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Document is not visible during time period that includes missed auto | ||
| // reload but is less than reloadPeriodInMs. | ||
| tick(2); | ||
| simulateVisibilityChange(false); | ||
| tick(3); | ||
| // No reload is dispatched. | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Dispatch a reload when next visible. | ||
| simulateVisibilityChange(true); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(2); | ||
|
|
||
| fixture.destroy(); | ||
| })); | ||
|
|
||
| it('does not reload when document becomes visible if there was not a missed reload', fakeAsync(() => { | ||
| store.setState( | ||
| createState( | ||
| createCoreState({ | ||
| reloadPeriodInMs: 5, | ||
| reloadEnabled: true, | ||
| }) | ||
| ) | ||
| ); | ||
| const fixture = TestBed.createComponent(ReloaderComponent); | ||
| fixture.detectChanges(); | ||
|
|
||
| tick(5); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Document is not visible during time period that does not include | ||
| // missed auto reload. | ||
| simulateVisibilityChange(false); | ||
| tick(3); | ||
| simulateVisibilityChange(true); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| fixture.destroy(); | ||
| })); | ||
|
|
||
| it('does not reload when document becomes visible if missed reload was already handled', fakeAsync(() => { | ||
| store.setState( | ||
| createState( | ||
| createCoreState({ | ||
| reloadPeriodInMs: 5, | ||
| reloadEnabled: true, | ||
| }) | ||
| ) | ||
| ); | ||
| const fixture = TestBed.createComponent(ReloaderComponent); | ||
| fixture.detectChanges(); | ||
|
|
||
| // Miss a reload because not visible. | ||
| simulateVisibilityChange(false); | ||
| tick(6); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(0); | ||
|
|
||
| // Dispatch a reload when next visible. | ||
| simulateVisibilityChange(true); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Document is not visible during time period that does not include | ||
| // another missed reload. | ||
| simulateVisibilityChange(false); | ||
| tick(2); | ||
| simulateVisibilityChange(true); | ||
| // No additional reload dispatched. | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(1); | ||
|
|
||
| fixture.destroy(); | ||
| })); | ||
|
|
||
| it('does not reload when document becomes visible if auto reload is off', fakeAsync(() => { | ||
| store.setState( | ||
| createState( | ||
| createCoreState({ | ||
| reloadPeriodInMs: 5, | ||
| reloadEnabled: false, | ||
| }) | ||
| ) | ||
| ); | ||
| const fixture = TestBed.createComponent(ReloaderComponent); | ||
| fixture.detectChanges(); | ||
|
|
||
| simulateVisibilityChange(false); | ||
| tick(5); | ||
|
|
||
| simulateVisibilityChange(true); | ||
| expect(dispatchSpy).toHaveBeenCalledTimes(0); | ||
|
|
||
| fixture.destroy(); | ||
| })); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.