Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/components / structure / Notification / Notification.spec.js
Size: Mime:
import React from 'react';
import { mount } from 'enzyme';
import Notification from './Notification';

/**
 * Wait for a specified amount of time.
 * @param {Number} msDelay the milliseconds to wait
 * @returns {Promise}
 */
const wait = msDelay => new Promise(resolve => setTimeout(resolve, msDelay));

describe('Notification', () => {
  it('should render content with role=alert', () => {
    const expectedContent = 'Content';
    const component = mount(<Notification>{expectedContent}</Notification>);
    const alert = component.find({ role: 'alert' });
    const content = component.text();

    expect(alert.length).toBe(1);
    expect(content).toEqual(expectedContent);
  });

  it('should render actions as a function', () => {
    const expectedActionComponent = <div className="myNotificationActionElement" />;
    const expectedContent = 'Content';

    const component = mount(<Notification actions={() => expectedActionComponent}>{expectedContent}</Notification>);
    const content = component.text();
    const actionElement = component.find('.myNotificationActionElement');

    expect(actionElement.length).toBe(1);
    expect(content).toEqual(expectedContent);
  });

  it('should render actions as a Node', () => {
    const expectedActionComponent = <div className="myNotificationActionElement" />;
    const expectedContent = 'Content';

    const component = mount(<Notification actions={expectedActionComponent}>{expectedContent}</Notification>);
    const content = component.text();
    const actionElement = component.find('.myNotificationActionElement');

    expect(actionElement.length).toBe(1);
    expect(content).toEqual(expectedContent);
  });

  it('should call onAutoClose when autoClose is set to true', async () => {
    expect.assertions(1);
    const autoCloseTimeout = 500;

    const mockFunction = jest.fn();

    mount(
      <Notification autoClose autoCloseTimeout={autoCloseTimeout} onAutoClose={mockFunction}>
        Content
      </Notification>
    );

    // Implementation detail alert: we need to wait for the hard-coded delay set in the
    // component before the callbacks are fired.
    await wait(autoCloseTimeout + 400);
    expect(mockFunction).toHaveBeenCalled();
  });

  it('should not call onAutoClose if autoClose is false', async () => {
    expect.assertions(1);
    const autoCloseTimeout = 500;

    const mockFunction = jest.fn();

    mount(
      <Notification autoClose={false} autoCloseTimeout={autoCloseTimeout} onAutoClose={mockFunction}>
        Content
      </Notification>
    );

    // Implementation detail alert: we need to wait for the hard-coded delay set in the
    // component before the callbacks are fired.
    await wait(autoCloseTimeout + 400);
    expect(mockFunction).not.toHaveBeenCalled();
  });
});