Skip to content

Instantly share code, notes, and snippets.

View Pyrolistical's full-sized avatar

Pyrolistical

View GitHub Profile
@Pyrolistical
Pyrolistical / service.spec.js
Created October 9, 2021 21:36
separate business layer from infrastructure layer before service.spec.js with jest mocks
import {jest} from '@jest/globals'
import Service from './service';
test('teams with disqualified players cannot check-in', async () => {
const playerDisqualificationsFindToArray = jest.fn()
.mockResolvedValueOnce([
{
teamID: 'some team id',
reason: 'was a big meanie'
@Pyrolistical
Pyrolistical / service.spec.js
Created October 9, 2021 21:17
separate business layer from infrastructure layer before service.spec.js
import Service from './service';
test('teams with disqualified players cannot check-in', async () => {
const db = {
collection(collection) {
switch (collection) {
case 'playerDisqualifications': {
return {
find({teamID}) {
expect(teamID).toBe('some team id');
@Pyrolistical
Pyrolistical / service.js
Last active October 9, 2021 20:56
separate business layer from infrastructure layer before service.js
export default (db) => {
return {
async checkIn(teamID) {
const disqualifiedPlayers = await db.collection('playerDisqualifications').find({
teamID
})
.toArray();
if (disqualifiedPlayers.length > 0) {
throw new Error('cannot check-in with disqualified players');
}
import {createContext, useContext} from 'react';
const LocaleContext = createContext();
export const ProvideLocale = Locale.Provider;
export const useLocale = () => {
const locale = useContext(LocaleContext);
return locale;
};
// React.memo caches the functional component if all the props are triple equals ===
const Increment = React.memo(({ caller, onClick }) => {
console.log(`${caller} button rendered`);
return <button onClick={onClick}>Increment</button>;
});
const BadComponent = () => {
const [count, setCount] = useState(0);
// declared functions never triple equals ===, even if its the same code
// ie. (() => {}) === (() => {}) is false,
// Good useMemo usage
const NormalComponent = ({ count }) => {
const costlyValue = useMemo(() => costlyFunction(count), [count]);
return <p>NormalComponent {costlyValue}</p>;
};
export default ({ count }) => {
// Slow component
const SlowComponent = () => {
const costlyValue = costlyFunction(count);
// BEFORE
const App = () => {
const [locale, setLocale] = useState('english');
return <Navigation locale={locale} />;
};
const Navigation = ({ locale }) => {
return <Link locale={locale} href="/about">About</Link>;
};
// BAD
export default ({ count }) => {
const Label = () => {
return <code>{count}</code>;
};
return <Label />;
};
// BAD
const LocaleContext = React.createContext();
const OverrideLocaleContext = React.createContext()
const App = () => {
const [locale, setLocale] = useState('english');
return <LocaleContext.Provider value={locale}>
<Navigation />
</LocaleContext.Provider>;
};
const BrokenComponent = (props) => {
useEffect(() => {
console.log('BrokenComponent called useEffect');
}, [props]); // THIS IS ALWAYS WRONG!
return <code>{JSON.stringify(props)}</code>;
};
const GoodComponent = (props) => {
useEffect(() => {