Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate auto-advance-after element. (#27574)
* Validate auto-advance-after element. * Unit tests.
- Loading branch information
Showing
2 changed files
with
207 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/** | ||
* Copyright 2020 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* 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 { | ||
AdvancementConfig, | ||
ManualAdvancement, | ||
MediaBasedAdvancement, | ||
TimeBasedAdvancement, | ||
} from '../page-advancement'; | ||
import {htmlFor} from '../../../../src/static-template'; | ||
|
||
describes.realWin('page-advancement', {amp: true}, (env) => { | ||
let html; | ||
let win; | ||
|
||
beforeEach(() => { | ||
html = htmlFor(env.win.document); | ||
win = env.win; | ||
}); | ||
|
||
describe('forElement', () => { | ||
describe('ManualAdvancement', () => { | ||
it('should return a manual advancement for an amp-story element', () => { | ||
const storyEl = win.document.createElement('amp-story'); | ||
const advancement = AdvancementConfig.forElement(win, storyEl); | ||
|
||
expect(advancement).to.be.instanceOf(ManualAdvancement); | ||
}); | ||
|
||
it('should not return a manual advancement for a non amp-story element', () => { | ||
const pageEl = win.document.createElement('amp-story-page'); | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.not.be.instanceOf(ManualAdvancement); | ||
}); | ||
}); | ||
|
||
describe('TimeBasedAdvancement', () => { | ||
it('should return a time advancement from ms unit', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="1000ms"> </amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.be.instanceOf(TimeBasedAdvancement); | ||
}); | ||
|
||
it('should return a time advancement from s unit', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="3s"> </amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.be.instanceOf(TimeBasedAdvancement); | ||
}); | ||
|
||
it('should not return a time advancement if no unit', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="3"> </amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.not.be.instanceOf(TimeBasedAdvancement); | ||
}); | ||
|
||
it('should not return a time advancement if random string', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="idkwhatimdoing"> </amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.not.be.instanceOf(TimeBasedAdvancement); | ||
}); | ||
}); | ||
|
||
describe('MediaBasedAdvancement', () => { | ||
it('should return a media advancement for amp-video with id', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="video-id"> | ||
<amp-story-grid-layer> | ||
<amp-video id="video-id"></amp-video> | ||
</amp-story-grid-layer> | ||
</amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.be.instanceOf(MediaBasedAdvancement); | ||
}); | ||
|
||
it('should return a media advancement for amp-video with data-id', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="video-id"> | ||
<amp-story-grid-layer> | ||
<amp-video data-id="video-id"></amp-video> | ||
</amp-story-grid-layer> | ||
</amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.be.instanceOf(MediaBasedAdvancement); | ||
}); | ||
|
||
it('should return a media advancement for amp-audio with id', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="audio-id"> | ||
<amp-story-grid-layer> | ||
<amp-audio id="audio-id"></amp-audio> | ||
</amp-story-grid-layer> | ||
</amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.be.instanceOf(MediaBasedAdvancement); | ||
}); | ||
|
||
it('should return a media advancement for amp-audio with data-id', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="audio-id"> | ||
<amp-story-grid-layer> | ||
<amp-audio data-id="audio-id"></amp-audio> | ||
</amp-story-grid-layer> | ||
</amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.be.instanceOf(MediaBasedAdvancement); | ||
}); | ||
|
||
it('should return a media advancement for amp-story-page with background audio', () => { | ||
const pageEl = html` | ||
<amp-story-page | ||
id="story-id" | ||
auto-advance-after="story-id" | ||
background-audio="foo.mp3" | ||
> | ||
</amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.be.instanceOf(MediaBasedAdvancement); | ||
}); | ||
|
||
it('should not return a media based advancement for other elements', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="div-id"> | ||
<amp-story-grid-layer> | ||
<div-id="div-id"></div> | ||
</amp-story-grid-layer> | ||
</amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.not.be.instanceOf(MediaBasedAdvancement); | ||
}); | ||
|
||
it('should not return a media based advancement if id does not exist', () => { | ||
const pageEl = html` | ||
<amp-story-page auto-advance-after="unknown-id"> | ||
<amp-story-grid-layer> | ||
<amp-video id="video-id"></amp-video> | ||
</amp-story-grid-layer> | ||
</amp-story-page> | ||
`; | ||
const advancement = AdvancementConfig.forElement(win, pageEl); | ||
|
||
expect(advancement).to.not.be.instanceOf(MediaBasedAdvancement); | ||
}); | ||
}); | ||
}); | ||
}); |