Skip to content

Instantly share code, notes, and snippets.

View pushkar100's full-sized avatar

Pushkar DK pushkar100

  • ThoughtSpot
  • Bangalore
  • 13:05 (UTC +05:30)
View GitHub Profile
/* testing/src/__tests__/helpers/index.test.js */
import { /* ... */ , setTraysForId, getTraysForId } from '../../helpers';
describe('Trays', () => {
beforeEach(() => {
window.localStorage.clear();
});
test('are stored locally on setting them via the id', () => {
/* testing/src/helpers/index.js */
// ...
const setTraysForId = (id, trays) => {
window.localStorage.setItem(`${id}`, JSON.stringify(trays));
}
const getTraysForId = (id) => {
return JSON.parse(window.localStorage.getItem(`${id}`));
/* src/__tests__/helpers/index.test.js */
import { isEarlierThanNow, fetchFakeAPIData } from '../../helpers';
// ...
test('fetchFakeAPIData, when requesting an animal, responds with a dog', (done) => {
// Arrange
const testRequest = { animal: true };
// Act
/* src/__tests__/helpers/index.test.js */
import { isEarlierThanNow, fetchFakeAPIData } from '../../helpers';
//...
test('fetchFakeAPIData, when requesting an animal, responds with a dog', async () => {
// Arrange
const testRequest = { animal: true };
// Act
/* src/__tests__/helpers/index.test.js */
import { isEarlierThanNow, fetchFakeAPIData } from '../../helpers';
//...
test('fetchFakeAPIData, when requesting an animal, responds with a dog', () => {
// Arrange
const testRequest = { animal: true };
// Act
/* /src/helpers/index.js */
// ...
const fetchFakeAPIData = (request) => new Promise((resolve, reject) => {
if (request.animal) {
resolve({
animal: 'Dog',
name: 'Charlie'
});
}
/* src/__tests__/helpers/index.test.js: */
import { isEarlierThanNow } from '../../helpers';
test('isEarlierThanNow, given a timestamp earlier than now, retruns true', () => {
// Arrange
const date = new Date('1/1/2000');
// Act
const actualIsEarlierDateThanNow = isEarlierThanNow(date);
@pushkar100
pushkar100 / earlier-than-now.js
Last active June 24, 2022 14:53
A helper module with a method that checks if a given timestamp is earlier than the current time
/* src/helpers/index.js: */
const isEarlierThanNow = (timestamp) => timestamp < Date.now();
export { isEarlierThanNow };
<ProductSearchFilter
products={["t-shirt", "shorts", "jeans"]}
colors={["red", "white", "black"]}
onSearch={(...args) => console.log(args)}
reducer={(state, action) => {
// 1. Run the default reducer:
const newState = productSearchFilterReducer(state, action);
// 2. Override its behaviour to fit your needs as a consumer:
if (
newState.product === "t-shirt" &&
import React, { useReducer } from "react";
// 1. REDUCER + HOOK:
const initialProductSearchFilterState = {
product: "",
colors: []
};
const productSearchFilterReducer = (state, action) => {
switch (action.type) {