-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support brightcove element (#445)
- Loading branch information
1 parent
99d226e
commit 17cebc2
Showing
15 changed files
with
229 additions
and
12 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
25 changes: 25 additions & 0 deletions
25
src/atoms/story-elements/brightcove/__snapshots__/brightcove.test.tsx.snap
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,25 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Brightcove should render default 1`] = ` | ||
<Fragment> | ||
<HelmetWrapper | ||
defer={true} | ||
encodeSpecialCharacters={true} | ||
> | ||
<script | ||
async={true} | ||
custom-element="amp-brightcove" | ||
src="https://cdn.ampproject.org/v0/amp-brightcove-0.1.js" | ||
/> | ||
</HelmetWrapper> | ||
<amp-brightcove | ||
data-account="1752604059001" | ||
data-embed="default" | ||
data-player="H1xW7NWcz" | ||
data-video-id="6156696074001" | ||
height="270" | ||
layout="responsive" | ||
width="480" | ||
/> | ||
</Fragment> | ||
`; |
41 changes: 41 additions & 0 deletions
41
src/atoms/story-elements/brightcove/brightcove.stories.tsx
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,41 @@ | ||
import React from "react"; | ||
import { storiesOf } from "@storybook/react"; | ||
import { Brightcove } from "./brightcove"; | ||
import { config, textStory } from "../../../__fixtures__"; | ||
import { Layout } from "../../layout"; | ||
|
||
const sampleBrightcoveElement = { | ||
description: "", | ||
"page-url": "/story/b7b6ecab-2b4f-4dc1-b04a-0b0ec76a50c4/element/2cfc6b4e-85fb-47b0-ae4e-580a650372f7", | ||
type: "external-file", | ||
"family-id": "0148ec01-3c7c-49ee-8a3f-db8ca4907dc8", | ||
title: "", | ||
"file-type": "video", | ||
id: "2cfc6b4e-85fb-47b0-ae4e-580a650372f7", | ||
url: null, | ||
"content-type": "video/brightcove", | ||
metadata: { | ||
"account-id": "1752604059001", | ||
"player-id": "default", | ||
"player-media": "default", | ||
"video-id": "6156696074001", | ||
"embed-code": | ||
"aHR0cHM6Ly9wbGF5ZXJzLmJyaWdodGNvdmUubmV0LzI3NjY2MjQ5MDUwMDEvZGVmYXVsdF9kZWZhdWx0L2luZGV4Lmh0bWw/dmlkZW9JZD02MzIzMDIxNjcxMTEy", | ||
"player-url": "//players.brightcove.net/2766624905001/default_default/index.html?videoId=6323021671112" | ||
}, | ||
subtype: "brightcove-video" | ||
}; | ||
|
||
const { metadata, ...sampleBrightcoveElementWithoutMetadata } = sampleBrightcoveElement; | ||
|
||
storiesOf("Brightcove Element", module) | ||
.addDecorator((story) => ( | ||
<Layout story={textStory} config={config}> | ||
{story()} | ||
</Layout> | ||
)) | ||
.add("Default", () => <Brightcove element={sampleBrightcoveElement} />) | ||
.add("With a square different layout", () => ( | ||
<Brightcove element={sampleBrightcoveElement} layout="fixed" width="400" height="400" /> | ||
)) | ||
.add("Brightcove without metadata", () => <Brightcove element={sampleBrightcoveElementWithoutMetadata} />); |
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,47 @@ | ||
import React from "react"; | ||
import { DefaultBrightcove, BrightcoveBase } from "./brightcove"; | ||
import { shallow } from "enzyme"; | ||
import { textStory, config } from "../../../__fixtures__"; | ||
|
||
const sampleBrightcoveElement = { | ||
description: "", | ||
"page-url": "/story/b7b6ecab-2b4f-4dc1-b04a-0b0ec76a50c4/element/2cfc6b4e-85fb-47b0-ae4e-580a650372f7", | ||
type: "external-file", | ||
"family-id": "0148ec01-3c7c-49ee-8a3f-db8ca4907dc8", | ||
title: "", | ||
"file-type": "video", | ||
id: "2cfc6b4e-85fb-47b0-ae4e-580a650372f7", | ||
url: null, | ||
"content-type": "video/brightcove", | ||
metadata: { | ||
"account-id": "1752604059001", | ||
"player-id": "H1xW7NWcz", | ||
"player-media": "default", | ||
"video-id": "6156696074001", | ||
"embed-code": | ||
"aHR0cHM6Ly9wbGF5ZXJzLmJyaWdodGNvdmUubmV0LzI3NjY2MjQ5MDUwMDEvZGVmYXVsdF9kZWZhdWx0L2luZGV4Lmh0bWw/dmlkZW9JZD02MzIzMDIxNjcxMTEy", | ||
"player-url": "//players.brightcove.net/2766624905001/default_default/index.html?videoId=6323021671112" | ||
}, | ||
subtype: "brightcove-video" | ||
}; | ||
const { metadata, ...sampleBrightcoveElementWithoutMeta } = sampleBrightcoveElement; | ||
|
||
describe("Brightcove", () => { | ||
it("should render default", () => { | ||
const wrapper = shallow(<DefaultBrightcove element={sampleBrightcoveElement} />); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
it("should call brightcoveElementRender when passed", () => { | ||
const brightcoveElementRender = jest.fn(); | ||
const modifiedConfig = { ...config, opts: { render: { storyElementRender: { brightcoveElementRender } } } }; | ||
const wrapper = shallow( | ||
<BrightcoveBase element={sampleBrightcoveElement} story={textStory} config={modifiedConfig} /> | ||
); | ||
expect(brightcoveElementRender.mock.calls.length).toBe(1); | ||
expect(wrapper.find(DefaultBrightcove).length).toBe(0); | ||
}); | ||
it("shouldn't render brightcove", () => { | ||
const wrapper = shallow(<DefaultBrightcove element={sampleBrightcoveElementWithoutMeta} />); | ||
expect(wrapper.find("amp-brightcove").length).toBe(0); | ||
}); | ||
}); |
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,79 @@ | ||
import React, { Fragment } from "react"; | ||
import Helmet from "react-helmet"; | ||
import { withStoryAndConfig } from "../../../context"; | ||
import get from "lodash.get"; | ||
import { BrightcoveElementTypes, DefaultBrightcoveElementTypes } from "./types"; | ||
|
||
export const DefaultBrightcove = ({ | ||
element, | ||
layout = "responsive", | ||
width = "480", | ||
height = "270" | ||
}: DefaultBrightcoveElementTypes) => { | ||
const { "account-id": accountId, "player-id": playerId = "default", "video-id": videoId } = get( | ||
element, | ||
["metadata"], | ||
{} | ||
); | ||
if (!accountId || !videoId || playerId === "default") { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Fragment> | ||
<Helmet> | ||
<script async custom-element="amp-brightcove" src="https://cdn.ampproject.org/v0/amp-brightcove-0.1.js" /> | ||
</Helmet> | ||
<amp-brightcove | ||
data-account={accountId} | ||
data-player={playerId} | ||
data-embed="default" | ||
data-video-id={videoId} | ||
layout={layout} | ||
width={width} | ||
height={height} | ||
/> | ||
</Fragment> | ||
); | ||
}; | ||
|
||
export const BrightcoveBase = ({ element, story, config }: BrightcoveElementTypes) => { | ||
const brightcoveElementRender = get( | ||
config, | ||
["opts", "render", "storyElementRender", "brightcoveElementRender"], | ||
null | ||
); | ||
|
||
return brightcoveElementRender ? ( | ||
brightcoveElementRender({ story, config, element }) | ||
) : ( | ||
<DefaultBrightcove element={element} /> | ||
); | ||
}; | ||
|
||
/** | ||
* Brightcove is a story element. | ||
* The look of the Brightcove can be changed using the render prop BrightcoveElementRender. In case BrightcoveElementRender is passed in the config, it is rendered. Otherwise a default Brightcove is rendered. | ||
* | ||
* @param {Object} params object containing parameters passed to the render prop | ||
* @param {Object} params.story story object | ||
* @param {Object} params.config config object | ||
* @param {Object} params.element the story element. This is same as the story element found in the story API response | ||
* | ||
* ```javascript | ||
* ... | ||
* ampRoutes(app, { | ||
* render: { | ||
* storyElementRender: { | ||
* brightcoveElementRender: ({ story, config, element }) => <MyCustomBrightcove story={story} config={config} storyElement={element} /> | ||
* } | ||
* } | ||
* }) | ||
* ... | ||
* ``` | ||
* | ||
* @category StoryElements | ||
* @module Brightcove | ||
* @component | ||
*/ | ||
export const Brightcove = withStoryAndConfig(BrightcoveBase); |
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,2 @@ | ||
import { Brightcove } from "./brightcove"; | ||
export { Brightcove }; |
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,13 @@ | ||
import { Common } from "../../../atoms/common-types"; | ||
import { Config } from "../../../types/config"; | ||
import { Story, StoryElement } from "../../../types/story"; | ||
|
||
export interface BrightcoveElementTypes { | ||
story: Story; | ||
config: Config; | ||
element: StoryElement; | ||
} | ||
|
||
export interface DefaultBrightcoveElementTypes extends Common { | ||
element: StoryElement; | ||
} |
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
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