Created
January 17, 2019 12:24
-
-
Save tonkotsuboy/9110451047bb78e51e0bcd541123da1d to your computer and use it in GitHub Desktop.
Hybrid Typesでモックを作る
This file contains 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 library = myLibrary(); | |
// library.foo(); | |
// Hybrid Typesでモックを作る | |
interface IMyLibrary { | |
(): void; | |
foo(): void; | |
bar(buz: number): string; | |
} | |
function getMockMyLibrary(): IMyLibrary { | |
const myLibrary = <IMyLibrary>() => {}; | |
myLibrary.foo = () => { | |
console.warn("モック1"); | |
}; | |
myLibrary.bar = buz => { | |
return `${buz}円だよ`; | |
}; | |
return myLibrary; | |
} | |
// 使ってみる | |
const serverSideLibrary = getMockMyLibrary(); | |
// 結果:"モック1" | |
serverSideLibrary.foo(); | |
// 結果"100000円だよ" | |
console.warn(serverSideLibrary.bar(100_000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment