-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Bento: Prepare Twitter Preact implementation #34194
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3e027f8
Do not override user given height until message gives one
caroqliu ecc598b
Support tweetid directly
caroqliu b05665f
Support bootstrap directly
caroqliu 4196e54
Add unit tests
caroqliu 29c347c
Support momentid directly
caroqliu 1e2fa4c
Add Storybook samples for additional options
caroqliu 476e126
Use Sinon syntax for spying on setter
caroqliu 4777757
Merge branch 'main' into preact-twitter
caroqliu 44a16c7
Merge branch 'main' into preact-twitter
caroqliu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,157 @@ | ||
/** | ||
* 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 * as Preact from '../../../../src/preact'; | ||
import {Twitter} from '../component'; | ||
import {WithAmpContext} from '../../../../src/preact/context'; | ||
import {createRef} from '../../../../src/preact'; | ||
import {mount} from 'enzyme'; | ||
import {serializeMessage} from '../../../../src/3p-frame-messaging'; | ||
import {waitFor} from '../../../../testing/test-helper'; | ||
|
||
describes.sandboxed('Twitter preact component v1.0', {}, (env) => { | ||
it('should render', () => { | ||
const wrapper = mount( | ||
tweetid="1356304203044499462" | ||
style={{ | ||
'width': '500px', | ||
'height': '600px', | ||
}} | ||
/> | ||
); | ||
|
||
const iframe = wrapper.find('iframe'); | ||
|
||
expect(iframe.prop('src')).to.equal( | ||
'http://ads.localhost:9876/dist.3p/current/frame.max.html' | ||
); | ||
expect(wrapper.find('iframe').prop('style').width).to.equal('100%'); | ||
expect(wrapper.find('iframe').prop('style').height).to.equal('100%'); | ||
}); | ||
|
||
it('should call given requestResize', () => { | ||
const requestResizeSpy = env.sandbox.spy(); | ||
const wrapper = mount( | ||
tweetid="1356304203044499462" | ||
style={{ | ||
'width': '500px', | ||
'height': '600px', | ||
}} | ||
requestResize={requestResizeSpy} | ||
/> | ||
); | ||
|
||
const iframe = wrapper.find('iframe').getDOMNode(); | ||
const sentinel = JSON.parse(iframe.getAttribute('name'))['attributes'][ | ||
'sentinel' | ||
]; | ||
|
||
const mockEvent = new CustomEvent('message'); | ||
mockEvent.data = serializeMessage('embed-size', sentinel, { | ||
'height': 1000, | ||
}); | ||
mockEvent.source = wrapper | ||
.getDOMNode() | ||
.querySelector('iframe').contentWindow; | ||
window.dispatchEvent(mockEvent); | ||
|
||
expect(requestResizeSpy).to.have.been.calledOnce; | ||
}); | ||
|
||
it('should change height', async () => { | ||
const wrapper = mount( | ||
tweetid="1356304203044499462" | ||
style={{ | ||
'width': '500px', | ||
'height': '600px', | ||
}} | ||
/> | ||
); | ||
|
||
const iframe = wrapper.find('iframe').getDOMNode(); | ||
const sentinel = JSON.parse(iframe.getAttribute('name'))['attributes'][ | ||
'sentinel' | ||
]; | ||
|
||
const mockEvent = new CustomEvent('message'); | ||
mockEvent.data = serializeMessage('embed-size', sentinel, { | ||
'height': 1000, | ||
}); | ||
mockEvent.source = wrapper | ||
.getDOMNode() | ||
.querySelector('iframe').contentWindow; | ||
window.dispatchEvent(mockEvent); | ||
|
||
wrapper.update(); | ||
|
||
await waitFor( | ||
() => wrapper.find('div').at(0).prop('style').height == 1000, | ||
'Height changes' | ||
); | ||
|
||
expect(wrapper.find('div').at(0).prop('style').height).to.equal(1000); | ||
}); | ||
|
||
it('should dispatch load event', async () => { | ||
const ref = createRef(); | ||
const onReadyState = env.sandbox.spy(); | ||
const wrapper = mount( | ||
ref={ref} | ||
tweetid="1356304203044499462" | ||
style={{ | ||
'width': '500px', | ||
'height': '600px', | ||
}} | ||
onReadyState={onReadyState} | ||
/> | ||
); | ||
|
||
let api = ref.current; | ||
expect(api.readyState).to.equal('loading'); | ||
expect(onReadyState).to.not.be.called; | ||
|
||
await wrapper.find('iframe').invoke('onLoad')(); | ||
api = ref.current; | ||
expect(api.readyState).to.equal('complete'); | ||
expect(onReadyState).to.be.calledOnce.calledWith('complete'); | ||
}); | ||
|
||
it('should reset iframe on pause', () => { | ||
const ref = createRef(); | ||
const wrapper = mount( | ||
<WithAmpContext playable={true}> | ||
ref={ref} | ||
tweetid="1356304203044499462" | ||
style={{ | ||
'width': '500px', | ||
'height': '600px', | ||
}} | ||
/> | ||
</WithAmpContext> | ||
); | ||
expect(wrapper.find('iframe')).to.have.lengthOf(1); | ||
|
||
const iframe = wrapper.find('iframe').getDOMNode(); | ||
const spy = env.sandbox.spy(iframe, 'src', ['set']); | ||
wrapper.setProps({playable: false}); | ||
expect(spy.set).to.be.calledOnce; | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to understand more about passing
bootstrap
andsrc
. It seems like it's very specific to AMP's concept of 3p frames. How would this be used in a Preact context?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! Ideally these will have default values so that they don't have to be provided and the component will "just work" -- however, that requires more file reorganization and isolation from AMP than the default behavior currently has. Until then, and when at that future point we do have defaults that are (potentially) tied to an AMP RTV, this is an example of how a publisher could provide alternative
bootstrap
orsrc
values to opt out of using logic served by AMP.I agree this isn't a great example of what publishers would use instead here, as it might look like recreating some of what we have in 3p/twitter.js and hosting it somewhere else (like on the publisher's own domain).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a preact context, would a 3p iframe be used at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear, so it would be helpful to have a working component to get feedback from. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Message received. Approval incoming