Skip to content

Instantly share code, notes, and snippets.

View getBookById.ts
interface BookDetailsViewModel {
title: string
is_available: boolean
}
export function getBookById(dependencies: Dependencies) {
return async (req: Request, res: Response) => {
try {
const result = await dependencies
.bookInformation(req.params.id)
View routes.ts
export const bookstoreRoutes = (app: Express, dependencies: Dependencies) => {
app.get('/books/:id', getBookById(dependencies))
}
View bookInformation.spec.ts
describe('Integration | Controller | Book information', () => {
let app: Express
let fakeDependencies: Dependencies
beforeEach(() => {
fakeDependencies = {
bookInformation: sinon.stub()
}
app = express()
bookstoreRoutes(app, fakeDependencies)
View bookInformationCard.tsx
export const BookInformationCard: FunctionComponent<Props> = ({ book }) => {
return book ? <p>{ book.title }</p> : <p>No book</p>
}
View bookInformationCard.spec.tsx
describe('Integration | Component | Book information Card', () => {
it('Matches to snapshot with a book', async () => {
const book = new BookDetails('title', true)
const { asFragment } = render(<BookInformationCard book={ book }/>)
await waitFor(() => {
expect(asFragment()).toMatchSnapshot()
})
})
View sequelize.customerLoader.test.ts
describe('Integration | Sequelize customer loader, () => {
const CUSTOMER_ID = 'id'
let customerLoader: CustomerLoader
beforeEach(async () => {
await SequelizeCustomerModel.create({ id: CUSTOMER_ID, name: 'name', email: 'email', phone: 'phone' })
customerLoader = new SequelizeCustomerLoader()
})
View http.basketRepository.test.ts
describe('Integration | Gateways | Http basket repository', () => {
const BASE_API_URL = 'https://www.fakeapi.com'
let httpClient: HTTPClient
let basketRepository: BasketRepository
let basket: Basket
beforeEach(() => {
httpClient = {
get : sinon.stub(),
post: sinon.stub()
View sequelize.customerLoader.ts
export class SequelizeCustomerLoader implements CustomerLoader {
async get(id: string): Promise<Customer> {
const customers = await SequelizeCustomerModel
.findAll({
where: { id: id },
limit: 1
})
.then(this.mapToCustomers)
return customers[0] || Promise.reject(new CustomerNotFoundError())
}
View sequelize.customerLoader.test.ts
describe('Integration | Gateways | Sequelize customer loader', () => {
const CUSTOMER_ID = 'id'
let customerLoader: CustomerLoader
beforeEach(async () => {
await SequelizeCustomerModel.create({
id : CUSTOMER_ID,
name : 'name',
email: 'email',
phone: 'phone'
View http.basketRepository.ts
interface BasketBody {
basket_items: string[]
}
interface BasketResponse {
basket_items: string[]
}
export class HTTPBasketRepository implements BasketRepository {
private endpoint = 'https://www.fakeapi.com/baskets'