Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #846

Merged
merged 15 commits into from
Sep 17, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ xdescribe('components/Form/CheckboxGroup and components/Form/CheckboxItem integr
checkboxes = subject.findAll('input[type=checkbox]');
});

xdescribe('binding to `inputValue`', () => {
xdescribe('when `inputValue` changes', () => {
describe('binding to `inputValue`', () => {
describe('when `inputValue` changes', () => {
it('the selected checkbox inputs change to match', () => {
const [optionOne, optionTwo] = checkboxes.wrappers.map(wrapper => wrapper.element);

Expand All @@ -59,7 +59,7 @@ xdescribe('components/Form/CheckboxGroup and components/Form/CheckboxItem integr
});
});

xdescribe('when a checkbox input is checked', () => {
describe('when a checkbox input is checked', () => {
it('it emits an `input` event with an array containing the values of the checked inputs', () => {
subject.setProps({ value: [] });
const [optionOne, optionTwo] = checkboxes.wrappers;
Expand All @@ -80,7 +80,7 @@ xdescribe('components/Form/CheckboxGroup and components/Form/CheckboxItem integr
});
});

xdescribe('CheckboxItem element IDs', () => {
describe('CheckboxItem element IDs', () => {
it('checkbox input IDs are unique', () => {
const [checkboxOne, checkboxTwo] = checkboxes.wrappers;
expect(checkboxOne.attributes('id')).not.toEqual(checkboxTwo.attributes('id'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ xdescribe('components/Form/RadioGroup and components/Form/RadioItem integration'
radios = subject.findAll('input[type=radio]');
});

xdescribe('binding to `inputValue`', () => {
xdescribe('when `inputValue` changes', () => {
describe('binding to `inputValue`', () => {
describe('when `inputValue` changes', () => {
it('the selected radio input changes to match', () => {
const [optionOne, optionTwo] = radios.wrappers.map(wrapper => wrapper.element);

Expand All @@ -51,7 +51,7 @@ xdescribe('components/Form/RadioGroup and components/Form/RadioItem integration'
});
});

xdescribe('when a radio input is checked', () => {
describe('when a radio input is checked', () => {
it('it emits an `input` event with the value of the checked radio', () => {
const [optionOne, optionTwo] = radios.wrappers;

Expand All @@ -69,7 +69,7 @@ xdescribe('components/Form/RadioGroup and components/Form/RadioItem integration'
});
});

xdescribe('RadioItem element IDs', () => {
describe('RadioItem element IDs', () => {
it('radio input IDs are unique', () => {
const [radioOne, radioTwo] = radios.wrappers;
expect(radioOne.attributes('id')).not.toEqual(radioTwo.attributes('id'));
Expand All @@ -81,7 +81,7 @@ xdescribe('components/Form/RadioGroup and components/Form/RadioItem integration'
});
});

xdescribe('radio inputs `name` attribute', () => {
describe('radio inputs `name` attribute', () => {
let nameOne, nameTwo;
beforeEach(() => {
[nameOne, nameTwo] = radios.wrappers.map(radio => radio.attributes('name'));
Expand Down
180 changes: 180 additions & 0 deletions tests/unit/components/DownloadLink.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { createTestSubject } from '../helpers';

const mockGetDownloadURL = jest.fn()
.mockName('getDownloadURL');
const mockRef = jest.fn()
.mockName('ref')
.mockReturnValue({
getDownloadURL: mockGetDownloadURL,
});

jest.mock('@firebase/app', () => ({
__esModule: true,
default: {
apps: [],
initializeApp: () => {},
auth: jest.fn(),
storage: jest.fn()
.mockImplementation(() => ({
ref: mockRef,
})),
},
})
);

jest.mock('@firebase/storage', () => ({
__esModule: true,
default: {
registerService: jest.fn(), // required by firebase/app
},
}));

import '@firebase/app';
import '@firebase/storage';

import DownloadLink from '@/components/DownloadLink';

describe('components/DownloadLink', () => {
let wrapper;
const mockProps = {
exerciseId: 'mock_id',
title: 'mock title',
fileName: 'mock_name',
};

beforeEach(() => {
wrapper = createTestSubject(DownloadLink, {
mocks: {},
stubs: [],
propsData: mockProps,
});
});

it('renders the component', () => {
expect(wrapper.exists()).toBe(true);
});

describe('lifecycle hooks', () => {
xdescribe('mounted', () => {
// const localVue = createLocalVue();
const mockGetDownloadURL = jest.fn()
.mockName('getDownloadURL');

const mockHref = 'mock href';

it('should call .getDownloadURL()', () => {
createTestSubject(DownloadLink, {
propsData: mockProps,
stubs: [],
mocks: {
getDownloadURL: mockGetDownloadURL,
},
});

expect(mockGetDownloadURL).toHaveBeenCalled();
});

it('should set linkHref if .getDownloadURL() returned download url', async () => {
expect.assertions(1);

const wrapper = createTestSubject(DownloadLink, {
mocks: {
getDownloadURL: mockGetDownloadURL
.mockReturnValue(mockHref),
},
stubs: [],
propsData: mockProps,
});

await wrapper.vm.$nextTick();
expect(wrapper.vm.linkHref).toEqual(mockHref);
});

it('should not set linkHref if .getDownloadURL() failed', async () => {
expect.assertions(1);

const wrapper = createTestSubject(DownloadLink, {
mocks: {
getDownloadURL: mockGetDownloadURL
.mockReturnValue(false),
},
stubs: [],
propsData: mockProps,
});

await wrapper.vm.$nextTick();
expect(wrapper.vm.linkHref).toBeEmpty();
});
});
});

describe('methods', () => {

xdescribe('getDownloadURL()', () => {
it('returns false if filename not set', async () => {
expect.assertions(1);

const result = await wrapper.vm.getDownloadURL();

expect(result).toBe(false);
});

describe('with filename', () => {
const mockFileName = 'mock file name';
const mockHref = 'mock href';

beforeEach(() => {
wrapper.setProps({
fileName: mockFileName,
});
});

it('calls storage().ref()', async () => {
expect.assertions(1);

await wrapper.vm.getDownloadURL();

expect(mockRef).toHaveBeenCalled();
});

it('calls fileRef.getDownloadURL()', async () => {
expect.assertions(1);

await wrapper.vm.getDownloadURL();

expect(mockGetDownloadURL).toHaveBeenCalled();
});

it('returns download url if fileRef.getDownloadURL() returned a valid response', async () => {
expect.assertions(1);

mockGetDownloadURL.mockReturnValue(mockHref);

const result = await wrapper.vm.getDownloadURL();

expect(result).toBe(mockHref);
});

it('returns false if fileRef.getDownloadURL() returned invalid response', async () => {
expect.assertions(1);

mockGetDownloadURL.mockReturnValue(null);

const result = await wrapper.vm.getDownloadURL();

expect(result).toBe(false);
});

it('returns false if fileRef.getDownloadURL() threw', async () => {
expect.assertions(1);
mockGetDownloadURL.mockImplementation(() => {
throw new Error('Error');
});
const result = await wrapper.vm.getDownloadURL();
expect(result).toBe(false);
});
});
});
});
});

Loading