Skip to content

Commit 7f758e3

Browse files
authored
Merge pull request #877 from abraham/about-block
Convert AboutBlock to LitElement
2 parents 534857f + 1130919 commit 7f758e3

File tree

5 files changed

+82
-29
lines changed

5 files changed

+82
-29
lines changed

__tests__/helpers/fixtures.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ import { render } from 'lit-html';
22

33
export const fixture = async <T extends import('lit-element').LitElement>(
44
html: import('lit-html').TemplateResult
5-
): Promise<{ element: T; shadowRoot: ShadowRoot }> => {
5+
): Promise<{ element: T; shadowRoot: ShadowRoot; container: HTMLDivElement }> => {
66
render(html, document.body);
77
const element = document.body.firstElementChild as T;
8-
if (!element || !element.shadowRoot) {
8+
9+
if (!element) {
910
throw new Error('Component not rendered');
1011
}
1112
await element.updateComplete;
12-
return { element, shadowRoot: element.shadowRoot };
13+
if (!element.shadowRoot) {
14+
throw new Error('ShadowDOM not rendered');
15+
}
16+
if (!element.shadowRoot.firstElementChild?.classList.contains('container')) {
17+
throw new Error('Container not rendered');
18+
}
19+
20+
return {
21+
element,
22+
shadowRoot: element.shadowRoot,
23+
container: element.shadowRoot.firstElementChild as HTMLDivElement,
24+
};
1325
};

src/components/about-block.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import './about-block';
2+
import { html } from 'lit-html';
3+
import { fixture } from '../../__tests__/helpers/fixtures';
4+
import { getByText, fireEvent } from '@testing-library/dom';
5+
import { uiActions } from '../redux/actions';
6+
import { mocked } from 'ts-jest/utils';
7+
8+
jest.mock('../redux/actions');
9+
10+
const toggleVideoDialogs = mocked(uiActions.toggleVideoDialog);
11+
12+
describe('about-block', () => {
13+
beforeEach(() => {
14+
toggleVideoDialogs.mockClear();
15+
});
16+
17+
it('defines a component', () => {
18+
expect(customElements.get('about-block')).toBeDefined();
19+
});
20+
21+
it('renders details', async () => {
22+
const { container } = await fixture(html`<about-block></about-block>`);
23+
expect(getByText(container, '{$ aboutBlock.title $}')).toBeDefined();
24+
expect(
25+
getByText(container, '{$ aboutBlock.callToAction.featuredSessions.description $}')
26+
).toBeDefined();
27+
expect(getByText(container, '{$ aboutBlock.statisticsBlock.attendees.number $}')).toBeDefined();
28+
expect(getByText(container, '{$ aboutBlock.statisticsBlock.attendees.label $}')).toBeDefined();
29+
});
30+
31+
it('plays the video', async () => {
32+
const { container } = await fixture(html`<about-block></about-block>`);
33+
fireEvent.click(getByText(container, '{$ aboutBlock.callToAction.howItWas.label $}'));
34+
expect(toggleVideoDialogs).toHaveBeenCalledTimes(1);
35+
expect(toggleVideoDialogs).toHaveBeenCalledWith({
36+
title: '{$ aboutBlock.callToAction.howItWas.title $}',
37+
youtubeId: '{$ aboutBlock.callToAction.howItWas.youtubeId $}',
38+
disableControls: true,
39+
opened: true,
40+
});
41+
});
42+
});

src/elements/about-block.ts renamed to src/components/about-block.ts

+16-24
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import '@polymer/iron-icon';
2-
import '@polymer/paper-button';
3-
import { html, PolymerElement } from '@polymer/polymer';
4-
import { ReduxMixin } from '../mixins/redux-mixin';
1+
import { customElement, html, css } from 'lit-element';
52
import { uiActions } from '../redux/actions';
6-
import './hoverboard-icons';
7-
import './shared-animations';
8-
9-
class AboutBlock extends ReduxMixin(PolymerElement) {
10-
static get template() {
11-
return html`
12-
<style include="shared-styles flex flex-alignment flex-reverse">
13-
:host {
14-
display: block;
15-
}
16-
3+
import { ThemedElement } from './themed-element';
4+
5+
@customElement('about-block')
6+
export class AboutBlock extends ThemedElement {
7+
static get styles() {
8+
return [
9+
...super.styles,
10+
css`
1711
.container {
1812
padding-top: 64px;
1913
}
@@ -61,8 +55,12 @@ class AboutBlock extends ReduxMixin(PolymerElement) {
6155
font-size: 56px;
6256
}
6357
}
64-
</style>
58+
`,
59+
];
60+
}
6561

62+
render() {
63+
return html`
6664
<div class="container">
6765
<div class="content">
6866
<div>
@@ -86,7 +84,7 @@ class AboutBlock extends ReduxMixin(PolymerElement) {
8684
<p>{$ aboutBlock.callToAction.howItWas.description $}</p>
8785
<paper-button
8886
class="animated icon-right"
89-
on-click="_playVideo"
87+
@click="${this.playVideo}"
9088
ga-on="click"
9189
ga-event-category="video"
9290
ga-event-action="watch"
@@ -139,11 +137,7 @@ class AboutBlock extends ReduxMixin(PolymerElement) {
139137
`;
140138
}
141139

142-
static get is() {
143-
return 'about-block';
144-
}
145-
146-
_playVideo() {
140+
private playVideo() {
147141
uiActions.toggleVideoDialog({
148142
title: '{$ aboutBlock.callToAction.howItWas.title $}',
149143
youtubeId: '{$ aboutBlock.callToAction.howItWas.youtubeId $}',
@@ -152,5 +146,3 @@ class AboutBlock extends ReduxMixin(PolymerElement) {
152146
});
153147
}
154148
}
155-
156-
window.customElements.define(AboutBlock.is, AboutBlock);

src/pages/home-page.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import '@polymer/iron-icon';
22
import { html, PolymerElement } from '@polymer/polymer';
3-
import '../elements/about-block';
3+
import '../components/about-block';
44
import '../elements/about-organizer-block';
55
import '../elements/featured-videos';
66
import '../elements/fork-me-block';

src/redux/actions.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,21 @@ declare global {
7474
}
7575
}
7676

77+
export interface VideoDialog {
78+
title: string;
79+
youtubeId: string;
80+
disableControls: boolean;
81+
opened: boolean;
82+
}
83+
7784
export const uiActions = {
7885
setViewportSize: (value) => {
7986
store.dispatch({
8087
type: SET_VIEWPORT_SIZE,
8188
value,
8289
});
8390
},
84-
toggleVideoDialog: (value = null) => {
91+
toggleVideoDialog: (value: VideoDialog) => {
8592
store.dispatch({
8693
type: TOGGLE_VIDEO_DIALOG,
8794
value,

0 commit comments

Comments
 (0)