Skip to content

Instantly share code, notes, and snippets.

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

Alex B bmdalex

🏠
Working from home
View GitHub Profile

Healthy Foods Inventory Challenge

Description

We are the owners of a vegan-friendly store and would like you to create a visual representation of our inventory.

There are two main task described in the sections below.

Feature 1: Display products in a rich text table

@bmdalex
bmdalex / RegistrationForm.spec.ts
Created January 24, 2021 21:57
RegistrationForm.spec.ts full test fine
import { createLocalVue, shallowMount } from '@vue/test-utils'
import { createBuilder, testData } from './RegistrationFormComponent.builder.js'
import RegistrationForm from './RegistrationForm.vue'
import { byTestId } from '@/utils/testUtils.js'
const localVue = createLocalVue()
const validEmail = 'somename@example.com'
const invalidEmail = 'invalid email'
const registerUserResponse = { status: 200 }
@bmdalex
bmdalex / RegistrationForm.spec.ts
Created January 24, 2021 21:37
DO use sentences that describe the behaviour of the component
describe('when the user submits a valid form', () => {
beforeEach(() => { ... })
it('should enable the submit button', { ... })
 it('should register the user by calling external API', () => { ... })
  it('should emit the "register-update" event', { ... })
})
@bmdalex
bmdalex / RegistrationForm.spec.ts
Created January 24, 2021 21:34
DON'T use names of internal properties
describe('handleFormSubmit', () => {
 beforeEach(() => { … })
it('button disabled attribute is false', { ... })
  it('is calling $auth.registerUser', () => { ... })
  it('is calling $emit("register-update", ...)', { ... })
})
@bmdalex
bmdalex / RegistrationForm.spec.ts
Created January 24, 2021 21:31
DO split tests using the frameworks' capabilities
describe('when the user submits a valid form', () => {
beforeEach(() => {
  /*
  * preconditions
  */
  })
it('should enable the submit button', () => {
expect(getFormSubmitButton(wrapper).attributes('disabled')).toBe(undefined)
  })
@bmdalex
bmdalex / RegistrationForm.spec.ts
Created January 24, 2021 21:29
DON'T create tests with many assertions (ideally just one assertion / test)
test('when the user submits a valid form should register the user', () => {
/*
* preconditions
*/
expect(getFormSubmitButton(wrapper).attributes('disabled')).toBe(undefined)
expect($auth.registerUser).toHaveBeenCalledWith({ email: validEmail })
expect(wrapper.emitted()['register-update'][0]).toBe(registerUserResponse)
})
@bmdalex
bmdalex / RegistrationForm.spec.ts
Last active January 24, 2021 21:29
DO group tests by behaviours (BDT) and preconditions
describe('when the user submits a valid form', () => {
beforeEach(() => { ... })
it('should enable the submit button', () => { ... })
it('should register the user by calling external API', () => { ... })
})
describe('when the user submits an invalid form', () => {
beforeEach(() => { ... })
it('should disable the submit button', () => { ... })
@bmdalex
bmdalex / RegistrationForm.spec.ts
Last active January 24, 2021 21:25
DON'T create a cluttered test suite with a flat structure
it('mounts properly', () => { ... })
it('when the user submits a form should register the user by calling external API',
() => { ... })
it('renders properly', () => { ... })
it('when the form is loaded should make the submit button disabled', () => { ... })
it('should log the appropriate error', () => { ... })
@bmdalex
bmdalex / RegistrationForm.spec.ts
Created January 24, 2021 21:21
DON'T test computed props or data props
it('should display the required fields form errors', () => {
expect(wrapper.vm.errorMsg).toBe('Invalid Email.')
})
@bmdalex
bmdalex / RegistrationForm.spec.ts
Created January 24, 2021 21:19
DO test calls to external functions (Vuex actions, APIs)
it('when the user submits it should call external API', () => {
expect($auth.registerUser).toHaveBeenCalledWith({
email: validEmail
})
})