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
@bmdalex
bmdalex / flattenArray.js
Last active November 17, 2018 01:40
citrusbyte problem
// Implementation:
const callIfArray = (arr, cb) => Array.isArray(arr) ? cb(arr) : arr;
const flattenRec = (arr) => arr.reduce(
(acc, curr) => acc.concat(callIfArray(curr, flattenRec)),
[]
);
const flattenArray = (arr) => callIfArray(arr, flattenRec);
// Testing:
const tests = [
@bmdalex
bmdalex / RegistrationForm.vue
Last active January 24, 2021 19:56
Registration Form Component used for testing
<script>
import { validateEmail } from './validateEmail.js'
export default {
name: 'RegistrationForm',
props: {
termsAndConditions: {
type: Boolean,
default: false
}
@bmdalex
bmdalex / RegistrationForm.vue
Last active January 24, 2021 20:19
Registration Form Component used for testing - short version
<script>
import { validateEmail } from './validateEmail.js'
export default {
name: 'RegistrationForm',
props: {
termsAndConditions: {
type: Boolean,
default: false
}
@bmdalex
bmdalex / RegistrationForm.spec.ts
Created January 24, 2021 21:12
DON'T test Vue methods
it('when the user submits it should call "handleFormSubmit" method', () => {
expect(wrapper.vm.handleFormSubmit).toHaveBeenCalled()
})
@bmdalex
bmdalex / RegistrationForm.spec.ts
Last active January 24, 2021 21:18
DO test rendered output
it('should display the required fields form errors', () => {
expect(getFormErrorElem(wrapper).text()).toBe('Invalid Email.')
})
@bmdalex
bmdalex / RegistrationForm.spec.ts
Created January 24, 2021 21:19
DO test events
it('when the user submits it should emit the "register-update" event', () => {
const eventArgs = wrapper.emitted()['register-update'][0]
expect(eventArgs).toBe(registerUserResponse)
})
@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
})
})
@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
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
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', () => { ... })