Skip to content

Instantly share code, notes, and snippets.

View xiongemi's full-sized avatar
🏠
Working from home

Emily Xiong xiongemi

🏠
Working from home
View GitHub Profile
@xiongemi
xiongemi / countries.selectors.spec.ts
Created December 1, 2020 04:44
unit test for selector file
import { FetchStatus } from '../models/fetch-status.enum';
import { mockInitialRootState } from '../root/root-state.mock';
import { countriesSelectors } from './countries.selectors';
describe('Countries Selectors', () => {
describe('initial state', () => {
it('should return countries', () => {
const actual = countriesSelectors.getCountries(mockInitialRootState);
const expected = [];
@xiongemi
xiongemi / use-field-errors-touched.hook.spec.ts
Created December 1, 2020 06:08
an example hook and unit test for it
import { renderHook } from '@testing-library/react-hooks';
import { useFieldErrorTouched } from './use-field-errors-touched.hook';
jest.mock('formik', () => ({
useFormikContext: () => {
return {
touched: { fieldName: true },
errors: { fieldName: 'random error' },
isSubmitting: true,
@xiongemi
xiongemi / countries.epics.spec.ts
Created December 1, 2020 06:59
unit test for epic
import { Action } from '@reduxjs/toolkit';
import {
countriesService,
mockCountriesResponse,
} from '@white-label-airline/services/countries';
import { ActionsObservable } from 'redux-observable';
import { of } from 'rxjs';
import { getCountriesEpic } from './countries.epics';
import { countriesSlice } from './countries.slice';
@xiongemi
xiongemi / <component-name>.props.ts
Last active December 10, 2020 03:17
ui component template for smart component
import { Dispatch } from '@reduxjs/toolkit';
export const mapStateToProps = (state: RootStateInterface) => {
return ...
};
export const mapDispatchToProps = (dispatch: Dispatch) => {
return ...
};
@xiongemi
xiongemi / 1. feature-name.enum.ts
Last active December 17, 2020 21:35
react feature toggle
export enum FeatureName {
ShowCountry = 'ShowCountry',
ShowCurrency = 'ShowCurrency',
}
@xiongemi
xiongemi / 1 i18n.service.ts
Last active March 15, 2023 23:25
react i18next setup
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpApi from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';
declare const process;
function initI18n(loadPath: string, defaultLanguage: string) {
return i18n
.use(LanguageDetector)
@xiongemi
xiongemi / use-locale.hook.ts
Created January 1, 2021 02:32
change material and date locale according to language
import {
Localization,
zhCN as materialZhCN,
enUS as materialEnUS,
} from '@material-ui/core/locale';
import { onLanguageChanged } from '@white-label-airline/services/i18n';
import { zhCN as dateZhCN, enGB as dateEnGB } from 'date-fns/locale';
import { useEffect, useState } from 'react';
const useLocale = () => {
import { WlaRootState } from '../root';
const getLanguage = (state: WlaRootState): string => {
return state.language;
};
export const languageSelectors = { getLanguage };
@xiongemi
xiongemi / index.ts
Last active October 3, 2022 15:50
Firebase cloud functions for update likes and dislikes
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
admin.initializeApp();
const database = admin.firestore();
function updateLikesDislikesCountOnPost(
change: functions.Change<FirebaseFirestore.DocumentSnapshot>,
collectionName: 'likes' | 'dislikes'
@xiongemi
xiongemi / blog-post-archive.tsx
Last active May 4, 2021 01:23
blog post archive just created
// Query all blog posts, display title and excerpt for each blog.
import { graphql } from 'gatsby';
import React from 'react';
export interface BlogPostArchiveProps {
data: {
allWpPost: {
nodes: {
id: string;
title: string;