Skip to content
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

Mini ch 5 #66

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a8b259f
:white_check_mark: :heavy_plus_sign: :art: :fire: Fork, cleanup and f…
cylcrow Feb 19, 2021
6466b34
:construction: :white_check_mark: :art: Added tests to assert compone…
cylcrow Feb 19, 2021
dca62d2
:recycle: Refactored names to comply with the specified in the challe…
cylcrow Feb 19, 2021
07be5b1
:recycle: :white_check_mark: :sparkles: :lipstick: Added base compone…
cylcrow Feb 20, 2021
d48c4e1
:sparkles: :recycle: :white_check_mark: :lipstick: Added Card component
cylcrow Feb 20, 2021
88e5f76
:sparkles: :recycle: :white_check_mark: :lipstick: Added Card component
cylcrow Feb 20, 2021
a3c7089
:lipstick: :art: Polished styles for Card component
cylcrow Feb 20, 2021
f7b8cf8
testing-libraries-cleanup: :recycle: Removed unnecesary dependencies …
cylcrow Feb 24, 2021
e5c9eac
:bento: :wheelchair: :recycle: Added roles to each element
cylcrow Mar 1, 2021
4547dca
:white_check_mark: :recycle: :sparkles: Added theming prototype
Mar 4, 2021
436964f
:construction: :recycle: Added theme wrapper for testing purposes
Mar 5, 2021
991f0b5
:recycle: :building_construction: :construction: Components isolation
Mar 5, 2021
be967d3
:recycle: :sparkles: :wheelchair: :alembic: Added VideoCards
Mar 5, 2021
7ed4920
:recycle: :wheelchair: :construction: Improved accesibility for Card …
Mar 5, 2021
671b550
:sparkles: :fire: :truck: :recycle: :art: :bug: :white_check_mark: Ad…
Mar 8, 2021
233f180
:recycle: :art: :sparkles: :truck: Adding youtube playback iframe
Mar 8, 2021
0fb5003
:recycle: :bug: :sparkles: :white_check_mark: Added video player and …
Mar 8, 2021
c1c945a
:sparkles: :bug: :construction: Added VideoPlayer feature to return t…
Mar 8, 2021
d3123dc
:white_check_mark: :fire: :art: Added remaining tests for mini ch 3
Mar 16, 2021
510fd3a
:fire: :recycle: :white_check_mark: Removed unnecessary ThemeContext …
Mar 16, 2021
cb565b0
:recycle: :fire: :white_check_mark: Added reducer for App, removed un…
Mar 17, 2021
c7c7f5e
:fire: :white_check_mark: Updated properties names
Mar 17, 2021
bbf8b0d
:bug: :white_check_mark: Solved issues loading videos by id on iframe
Mar 17, 2021
56c3147
:art: :lipstick: :white_check_mark: Added test for theme behavior for…
Mar 18, 2021
bb045c2
:art: :lipstick: :white_check_mark: Added test for theme behavior for…
Mar 18, 2021
9fb8e46
:art: :lipstick: :white_check_mark: Added test for theme behavior for…
Mar 18, 2021
f3239a6
:recycle: :white_check_mark: :art: Added tests for youtube search ser…
Mar 18, 2021
6a31647
:recycle: Moved generic components to higher path
Mar 22, 2021
24ac659
:sparkles: :recycle: :construction: Added routes for each feature
Mar 23, 2021
1380c53
:sparkles: Added login screen
Mar 24, 2021
a529834
:recycle: :zap: :bug: Fixed bug when videoId was updated on VideoPlay…
Mar 24, 2021
d35c358
:bug: Solved bug loading videos VideoPlayer>Home>VideoPlayer
Mar 25, 2021
5a2f82a
:sparkles: :recycle: :white_check_mark: Added Overlay and Menu compon…
Mar 25, 2021
2e93c5b
:sparkles: :recycle: :white_check_mark: Added Sidebar and buttons wit…
Mar 26, 2021
7de97f4
:sparkles: :recycle: :white_check_mark: Added favorites Add/Remove fe…
Mar 26, 2021
fb5c606
:sparkles: :recycle: :white_check_mark: Added tests and features to s…
Mar 26, 2021
b4808e1
:bug: Solved issues parsing/saving data from/to localStorage/sessionS…
Mar 26, 2021
c27bcd8
:recycle: :fire: Moved feature to generic component
Mar 26, 2021
49e9010
:recycle: :fire: :white_check_mark: Moved VideoPlayerContainer to gen…
Mar 26, 2021
72a6c2e
:sparkles: :recycle: :white_check_mark: Added login to a react portal
Mar 26, 2021
cd471e9
:sparkles: :recycle: :white_check_mark: Added favorites button to Sma…
Mar 26, 2021
ffa05de
:sparkles: :recycle: :white_check_mark: Added private routes
Mar 26, 2021
c8fff2a
:sparkles: :recycle: :rocket: Added last features
Mar 29, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_YOUTUBE_KEY=AIzaSyDFnO9VgkkdOBvHDq4NtEStIng-fAkEkEY
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"react/jsx-filename-extension": "error",
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
"react-hooks/exhaustive-deps": "warn",
"import/no-unresolved": ["off", { "ignore": [".css$"] }],
"import/prefer-default-export": "off",
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
# misc
.DS_Store
.env.local
.env.development
.env.development.local
.env.test
.env.test.local
.env.production.local

Expand Down
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/dom": "^7.29.6",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^10.4.9",
"@testing-library/react-hooks": "^5.1.0",
"@testing-library/user-event": "^12.1.3",
"debounce": "^1.2.0",
"jest-styled-components": "6.0.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3"
"react-scripts": "3.4.3",
"react-test-renderer": "^17.0.1",
"styled-components": "4.4.1"
},
"scripts": {
"start": "react-scripts start",
Expand All @@ -21,6 +27,7 @@
"lint:fix": "eslint ./src --ext .js,.jsx --fix"
},
"devDependencies": {
"@sheerun/mutationobserver-shim": "^0.3.3",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-eslint-comments": "^3.2.0",
Expand Down Expand Up @@ -57,5 +64,12 @@
"hooks": {
"pre-commit": "lint-staged"
}
},
"jest": {
"coveragePathIgnorePatterns": [
"src/utils/testing/helpers",
"src/utils/index.js",
"src/index.js"
]
}
}
13 changes: 13 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />

<script src="https://apis.google.com/js/api.js"></script>
<script>
gapi.load('client');
</script>
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
Expand All @@ -25,6 +30,14 @@
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = 'https://www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
</script>
<div id="root"></div>
<!--
This HTML file is a template.
Expand Down
Binary file added src/assets/icons/default_user.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/icons/hamburguer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 0 additions & 58 deletions src/components/App/App.component.jsx

This file was deleted.

80 changes: 80 additions & 0 deletions src/components/App/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React, { useReducer } from 'react';
import { LayoutWrapper } from '../Layout';
import { appReducer } from '../../reducers/appReducer';
import { lightTheme, darkTheme } from '../../providers/themes';
import youtubeSearchService from '../../services/youtubeSearchService';
import AppContext from '../../providers/AppContext';
import { createBrowserHistory } from "history";
import { Router } from 'react-router-dom';
import {
SET_CURRENT_VIDEO_PLAYBACK,
SET_IS_FIRST_LOAD,
SET_VIDEOS_LIST,
SWITCH_THEME
} from '../../reducers/actionTypes';
import Content from './Content';

const initialState = {
videosList: [],
currentVideoId: undefined,
isLightTheme: true,
isFirstLoad: true,
};

const history = createBrowserHistory();

function App() {
const [{
videosList,
isLightTheme,
isFirstLoad },
dispatch] = useReducer(appReducer, initialState);

const getThemeConfig = () => ({
theme: isLightTheme ? lightTheme : darkTheme,
switchTheme: () => dispatch({type: SWITCH_THEME}),
});

const search = async (query) => {
const serviceResponse = await youtubeSearchService.search(query);
serviceResponse.error
? dispatch({ type: SET_VIDEOS_LIST, payload: [] })
: dispatch({ type: SET_VIDEOS_LIST, payload: serviceResponse });
}

const firstSearch = async () => {
let counter = 0;
/* global gapi */
/* eslint no-undef: "error" */
while((!gapi || !gapi.client) && counter < 10){
counter+=1;
await new Promise(resolve => setTimeout(resolve, 100));
}
((!gapi || !gapi.client) || counter >= 10)
? dispatch({ type: SET_VIDEOS_LIST, payload: [] })
: search()
}

if(isFirstLoad){
dispatch({ type: SET_IS_FIRST_LOAD, payload: false });
firstSearch();
}

return (
<AppContext.Provider value={
{ ...getThemeConfig(),
search,
videosList,
playVideoById: (id) => dispatch({type: SET_CURRENT_VIDEO_PLAYBACK, payload: id})
}
}>
<Router history={history}>
<LayoutWrapper>
<Content />
</LayoutWrapper>
</Router>
</AppContext.Provider>
);
}

export default App;
96 changes: 96 additions & 0 deletions src/components/App/App.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React from 'react';
import { fireEvent, getByRole, act, render } from '@testing-library/react';
import { contextWrapper, googleMockedAPIObject, youtubeMockedData, YTMockedObject } from '../../utils';
import { getAllByTestId, getByTestId } from '@testing-library/dom';
import { darkTheme, lightTheme } from '../../providers/themes';
import AppContext from '../../providers/AppContext';
import App from './index';

global.gapi = googleMockedAPIObject();
global.YT = YTMockedObject;

const for400Miliseconds = () => new Promise((resolve) => {
setTimeout(() => resolve(), 400)
})

const build = async (Component = <App />) => {
let container;
const contextValue = { videos: youtubeMockedData.items, theme: lightTheme };
await act(async () => {
const wrapped = contextWrapper(AppContext, contextValue, Component);
container = render(wrapped).container;
});
return {
container,
LayoutWrapper: () => getByRole(container, 'application'),
ThemeSwitch: () => getByRole(container, 'switch'),
videosList: () => getAllByTestId(container, (testID) => testID.includes('video-card-') ),
noVideosAvailableCaption: () => getByTestId(container, 'no-videos-available' ),
unableToLoadVideos: () => getByTestId(container, 'unable-to-load-videos' ),
};
};

describe('App theme', () => {
it('applies "light" theme if none selected', async () => {
const wrapper = await build();
expect(wrapper.LayoutWrapper()).toHaveStyle(
`background: ${lightTheme.color.background}`
);
});

it('changes "light" theme to "dark" theme', async () => {
const wrapper = await build();

expect(wrapper.LayoutWrapper()).toHaveStyle(
`background: ${lightTheme.color.background}`
);
await act(async () => {
fireEvent.click(wrapper.ThemeSwitch());
});
expect(wrapper.LayoutWrapper()).toHaveStyle(
`background: ${darkTheme.color.background}`
);
});

it('shows videos list after successful fetch', async () => {
const built = (await build());
const { videosList } = built;
expect(videosList()).toHaveLength(youtubeMockedData.items.length);
});

it('shows "No hay videos :/" legend after failed fetch', async () => {
global.gapi = googleMockedAPIObject(false);
const built = (await build());
const { noVideosAvailableCaption } = built;
expect( noVideosAvailableCaption() ).toBeTruthy();
});

it('waits until `gapi` object is defined before first search', async () => {
global.gapi = undefined;
const built = await build();

await for400Miliseconds();
global.gapi = googleMockedAPIObject();
await act( async () => { await for400Miliseconds(); });

const { videosList } = built;
expect(videosList()).toHaveLength(youtubeMockedData.items.length);
});

it('shows "Couldn\'t load videos" when "gapi" object is not loaded after 1 second', async () => {
global.gapi = undefined;
const built = await build();

await for400Miliseconds();
await for400Miliseconds();

await act( async () => {
await for400Miliseconds();
global.gapi = googleMockedAPIObject();
});

const { noVideosAvailableCaption } = built;
expect(noVideosAvailableCaption()).toBeTruthy();
});
Comment on lines +38 to +98

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent 👏🏻


});
30 changes: 30 additions & 0 deletions src/components/App/App.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { act, getByRole, render } from '@testing-library/react';
import { googleMockedAPIObject } from '../../utils';
import App from './index';

global.gapi = googleMockedAPIObject();

const build = async (Component = <App />) => {
let container;
await act(async () => {
container = render(Component).container;
});
return {
container,
LayoutWrapper: () => getByRole(container, 'application'),
ThemeSwitch: () => getByRole(container, 'switch'),
};
};

describe('App layout', () => {
it('renders', async () => {
const wrapper = await build();
expect(wrapper).toMatchSnapshot();
});

it('displays layout wrapper', async () => {
const wrapper = await build();
expect(wrapper.LayoutWrapper()).toBeInTheDocument();
});
});
22 changes: 22 additions & 0 deletions src/components/App/Content.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import { Redirect, Route, Switch } from 'react-router';
import HomeVideos from '../HomeVideos/HomeVideos';
import VideoPlayerContainer from '../VideoPlayer/VideoPlayerContainer';

const Content = () => {
return (<div>
<Switch>
<Route exact path="/">
<Redirect to="/home"/>
</Route>
<Route path="/home">
<HomeVideos />
</Route>
<Route path="/player">
<VideoPlayerContainer />
</Route>
</Switch>
</div>);
}

export default Content;
Loading