Skip to content

Instantly share code, notes, and snippets.

@shlomokraus
shlomokraus / MessageService.spec.ts
Last active January 20, 2019 20:43
Demonstrate consuming Mockshot mocks
import { MessageService } from "../src/MessageService";
import { UserServiceMocks } from "../@mocks/mockshot-example/UserService";
describe("MessageService", () => {
const getUserResult = UserServiceMocks.getUser("success");
const userService = {
getUser: ()=>getUserResult
}
@shlomokraus
shlomokraus / MessageService.ts
Created January 20, 2019 20:26
Implementation of MessageService to deomnstrate Mockshot
import { UserService } from "./UserService";
export class MessageService {
constructor(private readonly userService: UserService){}
async prepareMessage(text: string, userId: string) {
const user = await this.userService.getUser(userId);
return {
to: user.email,
@shlomokraus
shlomokraus / UserService-Mockshot.ts
Last active January 20, 2019 20:43
Example of mockshot output for UserService
export class UserServiceMocks {
static getUser(mock: "success"): any {
switch (mock) {
case "success":
return {
"email": "[email protected]",
"id": "abc",
"name": "Some Name"
}
default:
@shlomokraus
shlomokraus / UserService.ts
Last active January 20, 2019 20:17
Example of UserService
export interface IUser {
id: string;
name: string;
email: string;
}
export class UserService {
constructor(private readonly db){}
@shlomokraus
shlomokraus / UserService.spec.ts
Last active January 20, 2019 20:23
Example of UserService test - without mockshot
import { UserService } from "../src/UserService";
describe("UserService", () => {
// fake database service
const db = {
findOneById: async (id) => {
const name = "Some Name";
const email = "[email protected]";
return {
@shlomokraus
shlomokraus / music-download-hooks.tsx
Last active February 24, 2019 07:24
Music download - hooks
/**
* Create app component (no container needed so it just mounts the page)
*/
function SongsApp() {
return <SongsPage />;
}
/**
* Page component (no props so fetch song list with useSongList hook)
@shlomokraus
shlomokraus / songs-app-redux.tsx
Last active November 24, 2018 17:46
Redux workflow
/**
* Create redux container
*/
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
fetchSongs: SongActions.fetchAll,
onDownload: SongActions.download
},