Skip to content

Instantly share code, notes, and snippets.

@jimkeller
jimkeller / html_nested_elements.html
Created September 11, 2022 21:02
Sample HTML Fixture for Jest Plain HTML Testing
export const nested_html =
`
<div id="test-immediate-children">
<div class="immediate-child">
Immediate Child 1
<div class="descendant-child">
Descendant 1-1
</div>
<div class="descendant-child">
Descendant 1-2
@jimkeller
jimkeller / nested_elements.test.ts
Last active September 11, 2022 21:08
Sample Jest test for raw HTML Markup
import { describe, expect, test, beforeEach } from '@jest/globals'
import { Renderer } from '../Renderer'
import { nested_html } from "../../test/fixtures/nested_html"
describe('Nested HTML Parser', () => {
let container: HTMLElement
let test_element: HTMLElement
let children: NodeList
@jimkeller
jimkeller / gist:fc60197ea1501886efb0721380090bf1
Last active September 19, 2022 03:51
Mocking scrollHeight in Jest
const my_element = document.getElementById('my-element')
jest.spyOn(my_element, "scrollHeight", 'get').mockReturnValue(100) //return 100 as scrollHeight
@jimkeller
jimkeller / gist:9a3946f3ca7841625d3d9167a0ed72f8
Last active September 19, 2022 03:51
mocking getBoundingClientRect in jest
const my_element = document.getElementById('#my-element)
jest.spyOn(my_element, "getBoundingClientRect").mockImplementation(
() => {
return new DOMRect(0, 0, 100, 500) //100px wide, 500px tall
}
)
@jimkeller
jimkeller / EmployeeDataProvider.js
Last active September 29, 2022 02:03
Example of dependency injection in React using Context
/**
*
* This file creates a React Context to hold the functionality for connecting to the
* employee data endpoint.
*
* I'm using Context here to mimic dependency injection so that the
* client can be more easily mocked for testing, or swapped out in the future.
*
* You can import EmployeeDataProvider from this file then
* wrapping a child component in <EmployeeDataProvider></EmployeeDataProvider> to give the component
@jimkeller
jimkeller / ContextWrappedComponent.js
Created September 29, 2022 01:49
Wrapping a React component in a custom context provider
/**
* This file will hold the Menu that lives at the top of the Page, this is all rendered using a React Component...
*
*/
import React, { useState } from 'react';
import EmployeeSearchForm from './EmployeeSearchForm';
import EmployeeDataProvider from "../services/EmployeeDataProvider";
function Header() {
@jimkeller
jimkeller / EmployeeSearchForm.js
Created September 29, 2022 01:55
EmployeeSearchForm using depdendency-injected service
function EmployeeSearchForm() {
const employee_data_client = useEmployeeDataClient();
const loadResults = async(employee_id) => {
return await employee_data_client.getResults(employee_id);
}
/* Below here, the rest of the component; e.g. rendering a form that calls
loadResults */
}
@jimkeller
jimkeller / EmployeeDataClient.js
Created September 29, 2022 02:01
Basic client service for use with React
import axios from "axios";
import regeneratorRuntime from "regenerator-runtime"; // eslint-disable-line no-unused-vars
const EmployeeDataClient = {
getResults: async(employee_id, endpoint_base_url) => {
endpoint_base_url = "http://localhost:3035/employees"
const response = await axios.get(`${endpoint_base_url}/${employee_id}`)
@jimkeller
jimkeller / debug-example.js
Last active September 29, 2022 02:18
NPM debug module in browser
import DebugModule from "debug";
//create a debug logger for my-component inside my-app
const debug = DebugModule("my-app:my-component");
//enable the my-app namespace for debugging
DebugModule.enable("my-app:*");
debug("This message will print now since my-app:* namespace is enabled for debugging");
@jimkeller
jimkeller / reactQueryWaitExample-1.js
Created October 14, 2022 01:39
react-query example for temporarily disabled query
/*
* call the useQuery hook, but leave the
* 'enabled' flag to false. Doing so prevents the query
* from running immediately on component mount.
*/
const { data, refetch, isError, isRefetching } = useQuery(
['search_results', inputValue],
async () => {
return await axios.get(`/path/to/results/api/${inputValue}`).catch((err) => { //handle error here })
},