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 1 commit
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
4 changes: 3 additions & 1 deletion src/components/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Router } from 'react-router-dom';
import {
SET_CURRENT_VIDEO_PLAYBACK,
SET_IS_FIRST_LOAD,
SET_SESSION,
SET_VIDEOS_LIST,
SWITCH_THEME
} from '../../reducers/actionTypes';
Expand Down Expand Up @@ -65,7 +66,8 @@ function App() {
{ ...getThemeConfig(),
search,
videosList,
playVideoById: (id) => dispatch({type: SET_CURRENT_VIDEO_PLAYBACK, payload: id})
playVideoById: (id) => dispatch({type: SET_CURRENT_VIDEO_PLAYBACK, payload: id}),
setUserSession: (data) => dispatch({type: SET_SESSION, payload: data}),
}
}>
<Router history={history}>
Expand Down
66 changes: 66 additions & 0 deletions src/components/Login/Login.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { useContext, useState } from "react";
import styled from "styled-components";
import loginService from '../../services/loginService';
import AppContext from "../../providers/AppContext";
import TextField from "../../ui/TextField/TextField";
import Button from "../../ui/Button/Button";
import { useHistory } from "react-router";

const StyledLogin = styled.div`
padding: 4px;
margin: 4px;
height: 400px;
width: 400px;
background: ${ ({theme}) => theme.color.surface };
color: ${ ({theme}) => theme.color.fontPrimary };
`

const StyledSection = styled.div`
display: grid;
height: min-content;
`;

const Login = () => {
const { push } = useHistory();
const { theme, setUserSession } = useContext(AppContext);
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");

const loginAttempt = async () => {
const response = await loginService(username, password);
setUserSession(response);
push({ pathname: "/home" });
}

return (<StyledLogin role="form" theme={theme}>
<StyledSection>
<label htmlFor="username">Usuario</label>
<TextField
id="username"
name="username"
data-testid="username-input"
onChange={(username) => setUsername(username)}
/>
</StyledSection>
<StyledSection>
<label htmlFor="password">Usuario</label>
<TextField
id="password"
name="password"
data-testid="password-input"
type="password"
onChange={(password) => setPassword(password)}
/>
</StyledSection>
<StyledSection>
<Button
data-testid="login-button"
onClick={loginAttempt}
>
Ingresar
</Button>
</StyledSection>
</StyledLogin>);
}

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

const build = async (Component = <Login/>, theme = lightTheme) => {
const contextValue = { theme, setUserSession: jest.fn() };
let routeWrap;
let container;
await act(async () => {
const wrappedContext = contextWrapper(AppContext, contextValue, Component);
routeWrap = await routerWrapper(wrappedContext);
container = render(routeWrap.wrap).container;
});
return {
container,
history: () => routeWrap.history,
usernameInput: () => getByTestId(container, 'username-input'),
passwordInput: () => getByTestId(container, 'password-input'),
loginButton: () => getByTestId(container, 'login-button'),
};
};

describe('Login screen', () => {
it('renders with lightTheme props', async () => {
const built = await build();
const { firstChild } = built.container;
expect(firstChild).toHaveStyle(`background: ${lightTheme.color.surface}`);
expect(firstChild).toHaveStyle(`color: ${lightTheme.color.fontPrimary}`);
});

it('renders with lightTheme props', async () => {
const built = await build(<Login/>, darkTheme);
const { firstChild } = built.container;
expect(firstChild).toHaveStyle(`background: ${darkTheme.color.surface}`);
expect(firstChild).toHaveStyle(`color: ${darkTheme.color.fontPrimary}`);
});

it('contains "username" and "password" inputs', async () => {
const built = await build();
const { usernameInput, passwordInput, loginButton } = built;
expect(usernameInput()).toBeDefined();
expect(passwordInput()).toBeDefined();
expect(loginButton()).toBeDefined();
});

it('redirects to home on successful login', async () => {
const REAL_USERNAME = "wizeline";
const REAL_PASSWORD = "Rocks!";
const built = await build();
const {
usernameInput,
passwordInput,
loginButton,
history
} = built;

fireEvent.change(usernameInput(), { target: { value: REAL_USERNAME } });
fireEvent.change(passwordInput(), { target: { value: REAL_PASSWORD } });
await act(async () => {
fireEvent.click(loginButton());
})
expect(history().location.pathname).toBe("/home");

});
})
24 changes: 24 additions & 0 deletions src/components/Login/Login.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { render } from '@testing-library/react';
import Login from './Login'
import { lightTheme } from '../../providers/themes';
import { contextWrapper } from '../../utils';
import AppContext from '../../providers/AppContext';

const build = (Component = <Login/>, theme = lightTheme) => {
const contextValue = { theme };
const wrappedContext = contextWrapper(AppContext, contextValue, Component);
const { container } = render(wrappedContext);
return { container };
};

describe('Login screen', () => {
it('renders with default properties', () => {
const { firstChild } = build().container;
expect(firstChild).toMatchSnapshot();
expect(firstChild).toHaveStyle('width: 400px');
expect(firstChild).toHaveStyle('height: 400px');
expect(firstChild).toHaveStyle('margin: 4px');
expect(firstChild).toHaveStyle('padding: 4px');
})
})
50 changes: 50 additions & 0 deletions src/components/Login/__snapshots__/Login.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Login screen renders with default properties 1`] = `
<div
class="sc-htpNat TqAvr"
role="form"
>
<div
class="sc-bxivhb YvQhC"
>
<label
for="username"
>
Usuario
</label>
<input
class="sc-bdVaJa fkGYjX"
data-testid="username-input"
id="username"
name="username"
/>
</div>
<div
class="sc-bxivhb YvQhC"
>
<label
for="password"
>
Usuario
</label>
<input
class="sc-bdVaJa fkGYjX"
data-testid="password-input"
id="password"
name="password"
type="password"
/>
</div>
<div
class="sc-bxivhb YvQhC"
>
<button
class="sc-bwzfXH mRSDS"
data-testid="login-button"
>
Ingresar
</button>
</div>
</div>
`;
3 changes: 2 additions & 1 deletion src/reducers/actionTypes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const SET_CURRENT_VIDEO_PLAYBACK = "SET_CURRENT_VIDEO_PLAYBACK";
export const SET_IS_FIRST_LOAD = "SET_IS_FIRST_LOAD";
export const SET_VIDEOS_LIST = "SET_VIDEOS_LIST";
export const SWITCH_THEME = "SWITCH_THEME";
export const SWITCH_THEME = "SWITCH_THEME";
export const SET_SESSION = "SET_SESSION";
5 changes: 5 additions & 0 deletions src/reducers/appReducer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
SET_CURRENT_VIDEO_PLAYBACK,
SET_IS_FIRST_LOAD,
SET_SESSION,
SET_VIDEOS_LIST,
SWITCH_THEME
} from './actionTypes'

const USER_SESSION_KEY = "user_session";
export const appReducer = (state, action) => {
const { type, payload } = action;
switch(type) {
Expand All @@ -18,6 +20,9 @@ export const appReducer = (state, action) => {
let isLightTheme = !state.isLightTheme;
return ({ ...state, isLightTheme });
};
case SET_SESSION:
window.sessionStorage.setItem(USER_SESSION_KEY, JSON.stringify(payload));
return ({ ...state, userSession: window.sessionStorage.getItem(USER_SESSION_KEY) });
default: return state;
}
}
25 changes: 21 additions & 4 deletions src/reducers/appReducer.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { appReducer } from './appReducer'
import { appReducer } from './appReducer';
import { userMockedData } from '../utils/';
import {
SET_CURRENT_VIDEO_PLAYBACK,
SET_IS_FIRST_LOAD,
SET_VIDEOS_LIST,
SWITCH_THEME
} from './actionTypes'
SWITCH_THEME,
SET_SESSION,
} from './actionTypes';

const USER_SESSION_KEY = "user_session";
const deepCopy = (stateObject) => JSON.parse(JSON.stringify(stateObject));
const originalState = {
videosList: [],
Expand Down Expand Up @@ -68,5 +71,19 @@ describe("app reducer", () => {
const action = { type: '?' };
let objectResult = appReducer(stateCopy, action);
expect(JSON.stringify(objectResult)).toEqual(JSON.stringify(originalState))
});
});

it('mutates `sessionData` storing session data on window only',() => {
const stateCopy = deepCopy(originalState);
const EXPECTED_SESSION_DATA = userMockedData;
const action = { type: SET_SESSION, payload: EXPECTED_SESSION_DATA };
let objectResult = appReducer(stateCopy, action);

expect(objectResult.videosList).toEqual(originalState.videosList);
expect(objectResult.currentVideoId).toEqual(objectResult.currentVideoId);
expect(objectResult.isLightTheme).toEqual(originalState.isLightTheme);
expect(objectResult.isFirstLoad).toEqual(originalState.isFirstLoad);
expect(objectResult.userSession).toEqual(JSON.stringify(EXPECTED_SESSION_DATA));
expect(window.sessionStorage.getItem(USER_SESSION_KEY)).toBe(JSON.stringify(EXPECTED_SESSION_DATA));
});
});
8 changes: 8 additions & 0 deletions src/services/loginService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { userMockedData } from '../utils/'
const loginService = async (username, password) => new Promise((resolve, reject) => {
if (username === 'wizeline' && password === 'Rocks!'){ resolve(userMockedData); }
reject(new Error('Username or password invalid'));
});


export default loginService;
24 changes: 24 additions & 0 deletions src/services/loginService.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import loginService from './loginService';

describe('loginService', () => {
it('returns error if bad userame and password is specified', async () => {
const FAKE_USERNAME = 'you_lost';
const FAKE_PASSWORD = 'the_game';
try{
await loginService(FAKE_USERNAME, FAKE_PASSWORD);
}catch(reason){
expect(reason.message).toBe("Username or password invalid");
}
});

it('returns "mockedUser" when real username and password is specified', async () => {
const REAL_USERNAME = 'wizeline';
const REAL_PASSWORD = 'Rocks!';
try{
const response = loginService(REAL_USERNAME, REAL_PASSWORD);
expect(JSON.stringify(response)).toEqual(JSON.stringify(response));
}catch{
return { passed: false };
}
});
});
19 changes: 19 additions & 0 deletions src/ui/Button/Button.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { useContext } from "react";
import styled from "styled-components";
import AppContext from "../../providers/AppContext";

const StyledButton = styled.button`
border-radius: 8px;
background: ${({theme}) => theme.color.secondary };
color: ${({theme}) => theme.color.fontPrimary };
`;

const Button = (props) => {
const { theme } = useContext(AppContext);
const condensedProps = { ...props, theme };
return (<StyledButton {...condensedProps}>
{ props.children }
</StyledButton>);
}

export default Button;
Loading