Skip to content

Commit

Permalink
fix(Tooltip single target):Added unit tests reactstrap#1185
Browse files Browse the repository at this point in the history
  • Loading branch information
akhlesh committed Apr 21, 2019
1 parent c340086 commit 8a82599
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/__tests__/PopperContent.spec.js
Expand Up @@ -42,6 +42,14 @@ describe('PopperContent', () => {
expect(wrapper.text()).toBe('Yo!');
});

it('should render children when isOpen is true and container is inline and DOM node passed directly for target', () => {
const targetElement = element.querySelector('#target');

const wrapper = mount(<PopperContent target={targetElement} isOpen container="inline">Yo!</PopperContent>);
expect(targetElement).toBeDefined();
expect(wrapper.text()).toBe('Yo!');
});

it('should render an Arrow in the Popper when isOpen is true and container is inline', () => {
const wrapper = mount(<PopperContent target="target" isOpen container="inline" arrowClassName="custom-arrow">Yo!</PopperContent>);

Expand Down
38 changes: 38 additions & 0 deletions src/__tests__/TooltipPopoverWrapper.spec.js
Expand Up @@ -375,6 +375,44 @@ describe('Tooltip', () => {
wrapper.detach();
});

describe('multi target', () => {
let targets, targetContainer;
beforeEach(() => {
targetContainer = document.createElement('div');
targetContainer.innerHTML = `<span class='example'>Target 1</span><span class='example'>Target 2</span>`
element.appendChild(targetContainer);
targets = targetContainer.querySelectorAll('.example');
});

afterEach(() => {
element.removeChild(targetContainer);
targets = null;
});

it("should attach tooltip on multiple target when a target selector matches multiple elements", () => {
const wrapper = mount(
<TooltipPopoverWrapper target=".example" isOpen={isOpen} toggle={toggle} delay={0}>Yo!</TooltipPopoverWrapper>,
{ attachTo: container });

targets[0].dispatchEvent(new Event('click'));
jest.runTimersToTime(0)
expect(isOpen).toBe(true);

targets[0].dispatchEvent(new Event('click'));
jest.runTimersToTime(0)
expect(isOpen).toBe(false);

targets[1].dispatchEvent(new Event('click'));
jest.runTimersToTime(0)
expect(isOpen).toBe(true);

targets[1].dispatchEvent(new Event('click'));
jest.runTimersToTime(0)
expect(isOpen).toBe(false);
wrapper.detach();
});
});

describe('delay', () => {
it('should accept a number', () => {
isOpen = true;
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/utils.spec.js
Expand Up @@ -116,6 +116,20 @@ describe('Utils', () => {
expect(spy).toHaveBeenCalled();
});

it('should return all matching elements if allElement param is true', () => {
const element = document.createElement('div');
element.innerHTML = `<span class='example'>span 1</span>
<span class='example'>span 2</span>`;
document.body.appendChild(element);

jest.spyOn(document, 'querySelectorAll');
const elements = Utils.getTarget('.example', true);
expect(elements.length).toEqual(2);
expect(elements[1].textContent).toEqual('span 2');
expect(document.querySelectorAll).toHaveBeenCalledWith('.example');
document.querySelectorAll.mockRestore();
});

it('should query the document for the target if the target is a string', () => {
const element = document.createElement('div');
element.className = 'thing';
Expand Down

0 comments on commit 8a82599

Please sign in to comment.