Created
June 27, 2019 22:56
-
-
Save JoelCodes/c67fba0a953d87543e4da5d2629d7a38 to your computer and use it in GitHub Desktop.
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 { RegionIso, CountryIso } from "./types"; | |
export async function getCountryIsos():Promise<CountryIso[]> { | |
return [ | |
{iso: "US", iso3: "USA", name: "United States"}, | |
{iso: "CA", iso3: "CAN", name: "Canada"}, | |
{iso: 'JP', iso3: 'JPN', name: 'Japan'} | |
] | |
} | |
export async function getRegionIsos():Promise<RegionIso[]>{ | |
return [ | |
{abbr: 'OR', name: 'Oregon', countryIso: 'US'}, | |
{abbr: 'CA', name: 'California', countryIso: 'US'}, | |
{abbr: 'BC', name: 'British Columbia', countryIso: 'CA'}, | |
{abbr: 'YT', name: 'Yukon Territory', countryIso: 'CA'}, | |
{abbr: 'TK', name: 'Tokyo Prefecture', countryIso: 'JP'} | |
] | |
} |
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
const theRealObject = {}; | |
console.log('Base Import Called'); | |
module.exports = { | |
theRealObject | |
} |
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
const import1 = require('./import1'); | |
const import2 = require('./import2'); | |
const someObj = {}; | |
const someOtherObj = {}; | |
console.log(someObj === someOtherObj); | |
console.log(import1.theRealObject === import2.theRealObject); |
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
const {theRealObject} = require('./baseImport'); | |
module.exports={theRealObject}; |
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
const {theRealObject} = require('./baseImport'); | |
module.exports={theRealObject}; |
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
module.exports = { | |
preset: 'ts-jest', | |
testEnvironment: 'node', | |
}; |
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
{ | |
"name": "node-imports", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "jest" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@types/jest": "^24.0.15", | |
"@types/react": "^16.8.22", | |
"@types/react-dom": "^16.8.4", | |
"jest": "^24.8.0", | |
"parcel-bundler": "^1.12.3", | |
"react": "^16.8.6", | |
"react-dom": "^16.8.6", | |
"react-hooks-testing-library": "^0.6.0", | |
"ts-jest": "^24.0.2", | |
"typescript": "^3.5.2" | |
} | |
} |
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
Show hidden characters
{ | |
"compilerOptions": { | |
"jsx": "react", | |
"lib": ["dom", "esnext"], | |
"types": ["jest"] | |
} | |
} |
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 interface CountryIso{ | |
iso: string; | |
iso3:string; | |
name:string; | |
} | |
export interface RegionIso { | |
abbr:string; | |
name:string; | |
countryIso: string; | |
} |
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 { makeLocationIsosHook } from "./useLocationIsos"; | |
import { RegionIso, CountryIso } from "./types"; | |
import {renderHook} from 'react-hooks-testing-library'; | |
async function getCountryIsos():Promise<CountryIso[]> { | |
return [ | |
{iso: "US", iso3: "USA", name: "United States"}, | |
{iso: "CA", iso3: "CAN", name: "Canada"}, | |
{iso: 'JP', iso3: 'JPN', name: 'Japan'} | |
] | |
} | |
async function getRegionIsos():Promise<RegionIso[]>{ | |
return [ | |
{abbr: 'OR', name: 'Oregon', countryIso: 'US'}, | |
{abbr: 'CA', name: 'California', countryIso: 'US'}, | |
{abbr: 'BC', name: 'British Columbia', countryIso: 'CA'}, | |
{abbr: 'YT', name: 'Yukon Territory', countryIso: 'CA'}, | |
{abbr: 'TK', name: 'Tokyo Prefecture', countryIso: 'JP'} | |
] | |
} | |
describe('useLocationIsos', () => { | |
it('Starts with base country isos and empty region isos', () => { | |
const useLocationIsos = makeLocationIsosHook(getCountryIsos, getRegionIsos) | |
const {result} = renderHook(useLocationIsos); | |
}); | |
}); |
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 { useState, useMemo, useEffect } from "react"; | |
import { CountryIso, RegionIso } from "./types"; | |
export interface LocationIsosHook { | |
countryIsos: CountryIso[]; | |
americanStates: RegionIso[]; | |
canadianProvinces: RegionIso[]; | |
allRegions:RegionIso[] | |
} | |
// Functional Dependency Injection | |
export function makeLocationIsosHook(getCountryIsos:() => Promise<CountryIso[]>, getRegionIsos: () => Promise<RegionIso[]>){ | |
return function useLocationIsos():LocationIsosHook{ | |
throw "Not Implemented Yet"; | |
// const [countryIsos, setCountryIsos] = useState<CountryIso[]>([ | |
// {iso: "US", iso3: "USA", name: "United States"}, | |
// {iso: "CA", iso3: "CAN", name: "Canada"} | |
// ]); | |
// const [americanStates, setAmericanStates] = useState<RegionIso[]>([]); | |
// const [canadianProvinces, setCanadianProvinces] = useState<RegionIso[]>([]); | |
// const allRegions = useMemo<RegionIso[]>(() => { | |
// return [...americanStates, ...canadianProvinces]; | |
// }, [americanStates, canadianProvinces]); | |
// useEffect(() => { | |
// getCountryIsos().then(setCountryIsos); | |
// getRegionIsos().then((regionIsos: RegionIso[]) => { | |
// setAmericanStates(regionIsos.filter(ri => ri.countryIso === "US")); | |
// setCanadianProvinces(regionIsos.filter(ri => ri.countryIso === "CA")); | |
// }) | |
// }, []); | |
// return { | |
// countryIsos, | |
// americanStates, | |
// canadianProvinces, | |
// allRegions | |
// } | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment