Guest User

Untitled

a guest
Oct 20th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import Vue from 'vue';
  2. import { defineFeature, loadFeature } from "jest-cucumber";
  3. import Quasar from "quasar-framework";
  4. import { mount, createLocalVue } from "@vue/test-utils";
  5. import LogIn from "@/components/LogIn.vue";
  6. import iconSet from "quasar-framework/icons/fontawesome";
  7. import "quasar-extras/fontawesome";
  8.  
  9. Vue.config.silent = true;
  10.  
  11. const feature = loadFeature("tests/unit/features/Login.feature");
  12.  
  13. defineFeature(feature, test => {
  14. let localVue;
  15.  
  16. /**
  17. * Initialize the Vue.js rendering engine with Quasar and font-awesome
  18. */
  19. beforeEach(() => {
  20. localVue = createLocalVue();
  21. localVue.use(Quasar, {
  22. config: {},
  23. iconSet: iconSet
  24. });
  25. });
  26.  
  27. test('Entering credentials and clicking the login button', ({ given, when, then }) => {
  28. // Define the wrapper at the higher scope so that it is accessible to all of the scenario blocks
  29. let wrapper;
  30.  
  31. given('an instance of our Login component', () => {
  32. wrapper = mount(LogIn, { localVue });
  33. });
  34.  
  35. given('a mocked implementation of the login method', () => {
  36. // Create a mocked function using Jest which will be used to replace the real implementation
  37. // of the login function from the component
  38. const login = jest.fn();
  39. wrapper.vm.login = login;
  40. });
  41.  
  42. when('I enter a username', () => {
  43. wrapper.find('input[type=text]').setValue('username');
  44. });
  45.  
  46. when('I enter a password', () => {
  47. wrapper.find('input[type=password]').setValue('password');
  48. });
  49.  
  50. when('I click the login button', () => {
  51. wrapper.find('button').trigger('click');
  52. });
  53.  
  54. then('I expect the login handler method to be executed', async () => {
  55. await wrapper.vm.$nextTick();
  56. expect(wrapper.vm.login).toHaveBeenCalled();
  57. });
  58. });
  59.  
  60. test('Logging in calls REST API client', ({ given, when, then }) => {
  61. let wrapper;
  62. let $api = {};
  63. let $router = {};
  64. const USERNAME = 'testusername';
  65. const PASSWORD = 'testpassword';
  66. const TOKEN = 'example_token';
  67.  
  68. given(/^a mock instance of the API client$/, () => {
  69. $api.userAuth = jest.fn((credentials, callback) => {
  70. expect(credentials.username).toEqual(USERNAME);
  71. expect(credentials.password).toEqual(PASSWORD);
  72. callback(null, TOKEN, {});
  73. });
  74. });
  75. given(/^a mock instance of the Vue router$/, () => {
  76. $router.push = jest.fn((pushOpts) => {
  77. expect(push.name).toEqual('home');
  78. expect(push.params.token).toEqual(TOKEN);
  79. });
  80. });
  81. given(/^an instance of the LogIn component$/, () => {
  82. wrapper = mount(LogIn, { localVue, mocks: { $api, $router }});
  83. });
  84. when(/^I enter a valid username$/, () => {
  85. wrapper.find('input[type=text]').setValue(USERNAME);
  86. });
  87. when(/^I enter a valid password$/, () => {
  88. wrapper.find('input[type=password]').setValue(PASSWORD);
  89.  
  90. });
  91. when(/^I click the login button$/, () => {
  92. wrapper.find('button.q-btn').trigger('click');
  93. });
  94. then(/^I expect that the username property is set correctly$/, () => {
  95. expect(wrapper.vm.$data.username).toEqual(USERNAME);
  96. });
  97. then(/^I expect that the password property is set correctly$/, () => {
  98. expect(wrapper.vm.$data.password).toEqual(PASSWORD);
  99. });
  100. then(/^I expect that the userAuth method on the API client is called$/, () => {
  101. expect($api.userAuth).toHaveBeenCalled();
  102. });
  103. then(/^I expect that the Vue router has been called to navigate away from the Login$/, async () => {
  104. await wrapper.vm.$nextTick();
  105. expect($router.push).toHaveBeenCalled();
  106. });
  107. });
  108. });
Add Comment
Please, Sign In to add comment