Skip to content

Instantly share code, notes, and snippets.

View itaditya's full-sized avatar
🎯
Focusing

Aditya Agarwal itaditya

🎯
Focusing
View GitHub Profile
const Aditya = () => (
<article>
<h1>Frontend Dev</h1>
<p>Builds Quality Software</p>
</article>
);
@itaditya
itaditya / links.md
Last active November 26, 2020 15:34
All the ways you can connect with me
@itaditya
itaditya / compound-comp-tab.jsx
Created October 10, 2020 05:21
Implementation of Tab component in compound component pattern.
const Tab = ({ id, children }) => (
<Consumer>
{({ changeTab }) => <div onClick={() => changeTab(id)}>{children}</div>}
</Consumer>
);
@itaditya
itaditya / App--Comp.jsx
Last active October 5, 2020 14:24
(Blog) Testing a Redux hooked app
// App.js
function App() {
const stateAPIStatus = useLoadFoodData();
return (
<div className="food-app">
<header>
<h1>Ordux</h1>
</header>
<Message status={stateAPIStatus} />
@itaditya
itaditya / useLoadFoodData-hooks.js
Last active October 5, 2020 14:23
(Blog) Build a Redux hooked app
function useLoadFoodData() {
const [stateAPIStatus, setAPIStatus] = useState('idle');
const dispatch = useDispatch();
useEffect(() => {
setAPIStatus('loading');
loadFoodData()
.then((data) => {
dispatch({
type: ACTIONS.LOAD_MENU,
@itaditya
itaditya / App.test.jsx
Last active October 5, 2020 13:27
(Blog) Testing a Redux hooked app
import { render, fireEvent, screen, waitForElementToBeRemoved } from '@testing-library/react';
import * as utils from './utils';
jest.mock('./utils');
const foodData = [
{
id: 'SM',
label: 'Sausage McMuffin',
@itaditya
itaditya / test-loading.js
Last active October 5, 2020 13:19
(Blog) Testing a Redux hooked app
import React from 'react';
import { Provider } from 'react-redux';
import { render, screen, waitForElementToBeRemoved } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import App from './App';
import { createReduxStore } from './redux';
describe('Test App', () => {
function renderApp(store = createReduxStore(), props = {}) {
@itaditya
itaditya / redux--react-wiring.js
Last active October 4, 2020 12:14
(Blog) Build a Redux hooked app
// redux.js
const enableReduxDevTools = window.__REDUX_DEVTOOLS_EXTENSION__?.();
export function createReduxStore() {
const store = createStore(foodReducer, enableReduxDevTools);
return store;
}
@itaditya
itaditya / example-test.js
Last active October 3, 2020 12:53
(Blog) Testing a Redux hooked app
test('only show veg food when veg filter is applied', () => {
// arrange
render(<App {...props} />);
// act
fireEvent.click(screen.getByRole('checkbox', { name: /Veg Only/i }));
// assert
expect(screen.queryByText(/Sausage McMuffin/i)).toBe(null);
expect(screen.getByText(/Mushroom Pizza/i)).toBeInTheDocument();
@itaditya
itaditya / Diet-App.jsx
Last active October 3, 2020 05:49
(Blog) Build a Redux hooked app
export default function App() {
const diet = useSelector((state) => state.diet);
const dispatch = useDispatch();
function handleVegToggle() {
dispatch({
type: ACTIONS.CHANGE_DIET,
});
}