Skip to content

Instantly share code, notes, and snippets.

function walkDOMTree(
root,
whatToShow = NodeFilter.SHOW_ALL,
{ inspect, collect, callback } = {}
) {
const walker = document.createTreeWalker(root, whatToShow, {
acceptNode(node) {
if (inspect && !inspect(node)) {
return NodeFilter.FILTER_REJECT;
}
@nicolasmendonca
nicolasmendonca / example.test.js
Last active July 4, 2021 18:16
Defines a function to mock window.fetch API and resolve or reject a custom response for tests
import {mockFetchResponse} from 'test-utils/mock-fetch-response';
test(`logging in displays the user's username`, async () => {
const {mockSuccessResponse, fetchPromise} = mockFetchResponse();
window.fetch = jest.fn().mockReturnValueOnce(fetchPromise);
render(<Login />)
const {username, password} = buildLoginForm()
userEvent.type(screen.getByLabelText(/username/i), username)
userEvent.type(screen.getByLabelText(/password/i), password)
@nicolasmendonca
nicolasmendonca / .eslintrc.js
Last active June 16, 2021 15:11
eslint setup for react and typescript
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
},
extends: [
'plugin:react/recommended',
'plugin:prettier/recommended',
'plugin:react-hooks/recommended',
@nicolasmendonca
nicolasmendonca / .pretterrc.json
Last active April 26, 2021 13:43
Create react npm package
{
"printWidth": 160,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always"
@nicolasmendonca
nicolasmendonca / withStateSlice.jsx
Created October 17, 2020 19:29
Prevents unnecessary re-renders using a state slice HOC
function withStateSlice(Comp, slice) {
const MemoComp = React.memo(Comp);
function Wrapper(props, ref) {
const state = useAppState();
const cell = slice(state, props);
return <MemoComp ref={ref} state={cell} {...props} />;
}
Wrapper.displayName = `withStateSlice(${Comp.displayName || Comp.name})`;
return React.memo(React.forwardRef(Wrapper));
}
@nicolasmendonca
nicolasmendonca / .babelrc
Created April 19, 2020 00:37
Basic node.js live reload config
{
"presets": [
"env"
]
}
@nicolasmendonca
nicolasmendonca / capitalize.test.js
Created November 30, 2019 13:16
String utilities
const capitalize = require('./index');
test('Capitalize is a function', () => {
expect(typeof capitalize).toEqual('function');
});
test('capitalizes the first letter of every word in a sentence', () => {
expect(capitalize('hi there, how is it going?')).toEqual(
'Hi There, How Is It Going?'
);
@nicolasmendonca
nicolasmendonca / .prettierrc
Created November 30, 2019 01:41
My preferred prettier setup
{
"singleQuote": true,
"useTabs": true,
"tabWidth": 2,
"trailingComma": "es5",
"endOfLine": "lf",
"printWidth": 80,
"semi": true
}
// We declare our mapper function, context aware
function mapProductId(transactionsArray) {
return transactionsArray
.map(transaction => ({
...transaction,
productId: this.getProductId(transaction.id)
}))
}
// And now, we can transform our data like so
const response3 = [
{ purchaseId: 21, productName: 'Bag' },
{ purchaseId: 22, productName: 'Lamp' },
];
const requiredOutput3 = [
{ productId: 31, purchaseId: 21, product: { name: 'Bag' } },
{ productId: 32, purchaseId: 22, product: { name: 'Lamp' } },
];