Skip to content

Instantly share code, notes, and snippets.

@InfoSec812
Last active October 22, 2018 19:09
Show Gist options
  • Select an option

  • Save InfoSec812/a2d870049a1b66a9adb4b79e0de4291c to your computer and use it in GitHub Desktop.

Select an option

Save InfoSec812/a2d870049a1b66a9adb4b79e0de4291c to your computer and use it in GitHub Desktop.
New scenario for our LogIn component
import Vue from 'vue';
import { defineFeature, loadFeature } from "jest-cucumber";
import Quasar from "quasar-framework";
import { mount, createLocalVue } from "@vue/test-utils";
import LogIn from "@/components/LogIn.vue";
import iconSet from "quasar-framework/icons/fontawesome";
import "quasar-extras/fontawesome";
Vue.config.silent = true;
const feature = loadFeature("tests/unit/features/Login.feature");
defineFeature(feature, test => {
let localVue;
/**
* Initialize the Vue.js rendering engine with Quasar and font-awesome
*/
beforeEach(() => {
localVue = createLocalVue();
localVue.use(Quasar, {
config: {},
iconSet: iconSet
});
});
test('Entering credentials and clicking the login button', ({ given, when, then }) => {
// SNIP - This is the test from our previous post
});
test('Logging in calls REST API client', ({ given, when, then }) => {
let wrapper;
let $api = {};
let $router = {};
const USERNAME = 'testusername';
const PASSWORD = 'testpassword';
const TOKEN = 'example_token';
given(/^a mock instance of the API client$/, () => {
$api.userAuth = jest.fn((credentials, callback) => {
expect(credentials.username).toEqual(USERNAME);
expect(credentials.password).toEqual(PASSWORD);
callback(null, TOKEN, {});
});
});
given(/^a mock instance of the Vue router$/, () => {
$router.push = jest.fn((pushOpts) => {
expect(pushOpts.name).toEqual('home');
expect(pushOpts.params.token).toEqual(TOKEN);
});
});
given(/^an instance of the LogIn component$/, () => {
wrapper = mount(LogIn, { localVue, mocks: { $api, $router }});
});
when(/^I enter a valid username$/, () => {
wrapper.find('input[type=text]').setValue(USERNAME);
});
when(/^I enter a valid password$/, () => {
wrapper.find('input[type=password]').setValue(PASSWORD);
});
when(/^I click the login button$/, () => {
wrapper.find('button.q-btn').trigger('click');
});
then(/^I expect that the username property is set correctly$/, () => {
expect(wrapper.vm.$data.username).toEqual(USERNAME);
});
then(/^I expect that the password property is set correctly$/, () => {
expect(wrapper.vm.$data.password).toEqual(PASSWORD);
});
then(/^I expect that the userAuth method on the API client is called$/, () => {
expect($api.userAuth).toHaveBeenCalled();
});
then(/^I expect that the Vue router has been called to navigate away from the Login$/, async () => {
await wrapper.vm.$nextTick();
expect($router.push).toHaveBeenCalled();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment