Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cramforce committed Jun 13, 2020
1 parent 9f0cb54 commit 4fa40e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/service/viewer-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ import {
serializeQueryString,
} from '../url';
import {isIframed} from '../dom';
import {listen} from '../event-helper';
import {map} from '../utils/object';
import {registerServiceBuilderForDoc} from '../service';
import {reportError} from '../error';
import {urls} from '../config';
import {listenOnce} from '../event-helper';

const TAG_ = 'Viewer';

Expand Down Expand Up @@ -902,9 +902,9 @@ export class ViewerImpl {
passive: true,
};
unlisten.push(
listenOnce(this.win, 'keydown', makeVisible, options),
listenOnce(this.win, 'touchstart', makeVisible, options),
listenOnce(this.win, 'mousedown', makeVisible, options)
listen(this.win, 'keydown', makeVisible, options),
listen(this.win, 'touchstart', makeVisible, options),
listen(this.win, 'mousedown', makeVisible, options)
);
this.whenFirstVisible().then(doUnlisten);
}
Expand Down
34 changes: 34 additions & 0 deletions test/unit/test-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ describes.sandboxed('Viewer', {}, (env) => {
ancestorOrigins: null,
search: '',
};
windowApi.addEventListener = (type, listener) => {
events[type] = listener;
};
windowApi.removeEventListener = (type, listener) => {
expect(events[type]).to.equal(listener);
delete events[type];
};
windowApi.document = {
nodeType: /* DOCUMENT */ 9,
defaultView: windowApi,
Expand Down Expand Up @@ -646,6 +653,33 @@ describes.sandboxed('Viewer', {}, (env) => {
});
});

describe('User action makes doc visible', () => {
const eventTest = (eventName) => {
ampdoc.overrideVisibilityState('prerender');
viewer = new ViewerImpl(ampdoc);
expect(ampdoc.getVisibilityState()).to.equal('prerender');

expect(events.touchstart).to.not.be.undefined;
expect(events.keydown).to.not.be.undefined;
expect(events.mousedown).to.not.be.undefined;

events[eventName]();
expect(ampdoc.getVisibilityState()).to.equal('visible');
expect(events.touchstart).to.be.undefined;
expect(events.keydown).to.be.undefined;
expect(events.mousedown).to.be.undefined;
};

it(
'should become visible on touchstart',
eventTest.bind(null, 'touchstart')
);

it('should become visible on mousedown', eventTest.bind(null, 'mousedown'));

it('should become visible on keydown', eventTest.bind(null, 'keydown'));
});

describe('Messaging not embedded', () => {
it('should not expect messaging', () => {
expect(viewer.messagingReadyPromise_).to.be.null;
Expand Down

0 comments on commit 4fa40e7

Please sign in to comment.