|
| 1 | +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +async function createIframe(): Promise<HTMLIFrameElement> { |
| 17 | + return new Promise<HTMLIFrameElement>((resolve) => { |
| 18 | + const iframe = document.createElement('iframe') as HTMLIFrameElement; |
| 19 | + document.body.appendChild(iframe); |
| 20 | + iframe.src = './testable-iframe.html'; |
| 21 | + iframe.onload = () => resolve(iframe); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +describe('plugin lib integration', () => { |
| 26 | + const {expect} = chai; |
| 27 | + |
| 28 | + beforeEach(async function() { |
| 29 | + this.sandbox = sinon.sandbox.create({useFakeServer: true}); |
| 30 | + this.sandbox.server.respondImmediately = true; |
| 31 | + this.iframe = await createIframe(); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(function() { |
| 35 | + document.body.removeChild(this.iframe); |
| 36 | + this.sandbox.restore(); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('tb.plugin.lib.run', () => { |
| 40 | + describe('#getRuns', () => { |
| 41 | + it('returns list of runs', async function() { |
| 42 | + this.sandbox |
| 43 | + .stub(tf_backend.runsStore, 'getRuns') |
| 44 | + .returns(['foo', 'bar', 'baz']); |
| 45 | + |
| 46 | + const runs = await (this.iframe.contentWindow as any).getRuns(); |
| 47 | + |
| 48 | + expect(runs).to.deep.equal(['foo', 'bar', 'baz']); |
| 49 | + }); |
| 50 | + }); |
| 51 | + describe('#addRunsChangeListener', () => { |
| 52 | + it('lets plugins to subscribe to runs change', async function() { |
| 53 | + const runsChanged = this.sandbox.stub(); |
| 54 | + const promise = new Promise((resolve) => { |
| 55 | + (this.iframe.contentWindow as any).addRunsChangeListener(resolve); |
| 56 | + }).then(runsChanged); |
| 57 | + this.sandbox.server.respondWith([ |
| 58 | + 200, |
| 59 | + {'Content-Type': 'application/json'}, |
| 60 | + '["foo", "bar"]', |
| 61 | + ]); |
| 62 | + |
| 63 | + await tf_backend.runsStore.refresh(); |
| 64 | + await promise; |
| 65 | + |
| 66 | + expect(runsChanged).to.have.been.calledOnce; |
| 67 | + expect(runsChanged).to.have.been.calledWith(['foo', 'bar']); |
| 68 | + }); |
| 69 | + }); |
| 70 | + }); |
| 71 | +}); |
| 72 | +1; |
0 commit comments