This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BAD | |
const LocaleContext = React.createContext(); | |
const OverrideLocaleContext = React.createContext() | |
const App = () => { | |
const [locale, setLocale] = useState('english'); | |
return <LocaleContext.Provider value={locale}> | |
<Navigation /> | |
</LocaleContext.Provider>; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {createContext, useContext} from 'react'; | |
const LocaleContext = createContext(); | |
export const ProvideLocale = Locale.Provider; | |
export const useLocale = () => { | |
const locale = useContext(LocaleContext); | |
return locale; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default (db) => { | |
return { | |
async findDisqualifiedPlayers(teamID) { | |
return db.collection('playerDisqualifications').find({ | |
teamID | |
}) | |
.toArray(); | |
}, | |
async checkInTeam(teamID) { | |
return db.collection('checkIns').insert({ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {jest} from '@jest/globals' | |
import Service from './service'; | |
test('teams with disqualified players cannot check-in', async () => { | |
const repository = { | |
findDisqualifiedPlayers: jest.fn() | |
.mockResolvedValueOnce([ | |
{ | |
teamID: 'some team id', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async checkInTeam(teamID) { | |
return db.collection('teams').updateOne({ | |
_id: teamID | |
}, { | |
$set: { | |
checkedInAt: new Date() | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash'; | |
export class InsufficientStock extends Error { | |
constructor(message) { | |
super(message); | |
this.name = 'InsufficientStock'; | |
} | |
} | |
export default (repository) => { |