By user7029167


2018-02-13 13:58:29 8 Comments

I am extending a Library Function in a Class and updating the Methods. But I am not able to write a Unit test case. This is my JS file of comonent. During instantiation i am providing reference of a table and a function to dispatch actions for update the redux-store and reflect the changes according.

Customized Table.js

import {Table} from 'third-partyLibrary';

const vendorMatch = (typeof Element !== 'undefined'
  && (Element.prototype.matches || Element.prototype.matchesSelector
    || Element.prototype.webkitMatchesSelector || Element.prototype.mozMatchesSelector
    || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector));

function matches(el, query) {
  if (vendorMatch) return vendorMatch.call(el, query);
  let nodes = el.parentNode ? el.parentNode.querySelectorAll(query) : [];
  for (let i = 0; i < nodes.length; i++) {
    if (nodes[i] === el) return true;
  }
  return false;
}

function getParent(_parent, query, _limitEl) {
  let limitEl = _limitEl instanceof Array ? _limitEl : [_limitEl || document.body];
  let parent = _parent;
  while (parent) {
    if (matches(parent, query)) {
      return parent;
    }
    if (limitEl.indexOf(parent) !== -1) {
      return false;
    }
    parent = parent.parentNode;
  }
  return false;
}

class CustomizedTable extends Table {
  constructor(el, onRowClick = (code) => {
    return code;
  }, extraParams = {}) {
    super(el, extraParams);
    this._onRowClick = onRowClick;
  }

  _onClick(...args) {
    const [e] = args;
    if (e.target.type === 'button' || e.target.parentElement.type === 'button') {
      return;
    }
    const target = getParent(e.target || e.srcElement, 'tbody tr', this.el);
    const activeRows = this.getActiveRows();
    super._onClick(...args);
    if (activeRows.length) {
      this._onRowClick(activeRows[0].dataset.timezoneCode, false);
    }
    if (target.dataset) {
      this._onRowClick(target.dataset.timezoneCode, true);
    }
  }
}
export default CustomizedTable;

and This is What am I trying :(

import React from 'react';
import {mount} from 'enzyme';
import CustomizedTable from './CustomizedTable';

jest.mock('third-party/js/table');



describe('Cutom Table', () => {
  const onRowClick = jest.fn();

  const el = (document.body.innerHTML =  "<table> <tbody> " +
    "<tr data-timezone-code='PO01' className='timeZone'> " +
  "<td className='code'>PO01</td>" +
  " <td className='description'><span title='VATICAN CITY'>VATICAN CITY</span></td> " +
  "<td className='offset'><span title='GMT -1:0'>GMT -1:0</span></td> " +
  "<td className='actions' /> " +
  "</tr> " +
    "</tbody> " +
    "</table>");

  const extraParams = {};

  let TimeZone = new CustomizedTable(el, onRowClick, extraParams);


  it('Should Render Table', () => {
    const wrapper = mount(<TimeZone/>);

    const row = wrapper.find('.timezone');
    expect(row.exists()).toBe(true);
    expect(row.length).toBe(1);

    wrapper.simulate('click');
    expect(onRowClick).toHaveBeenCalled();

0 comments

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] What is Unit test, Integration Test, Smoke test, Regression Test?

24 Answered Questions

[SOLVED] How should I unit test threaded code?

31 Answered Questions

[SOLVED] Unit Testing C Code

7 Answered Questions

[SOLVED] Writing unit tests in Python: How do I start?

13 Answered Questions

[SOLVED] JavaScript unit test tools for TDD

32 Answered Questions

[SOLVED] How do you unit test private methods?

13 Answered Questions

[SOLVED] Unit tests vs Functional tests

30 Answered Questions

12 Answered Questions

[SOLVED] Unit test naming best practices

44 Answered Questions

[SOLVED] Is Unit Testing worth the effort?

  • 2008-09-15 21:44:00
  • Ross Fuhrman
  • 248474 View
  • 573 Score
  • 44 Answer
  • Tags:   unit-testing

Sponsored Content