import React from 'react';
import '@testing-library/jest-dom/extend-expect';
import { render } from '@testing-library/react';
import { Link } from '../../Link';
import { Notification } from './index';
var NOW = '2019-11-05T11:00:00.000Z';
describe('Notification', function () {
  var wrapper;
  beforeEach(function () {
    jest.spyOn(Date, 'now').mockImplementation(function () {
      return new Date(NOW).valueOf();
    });
  });
  describe('when the notification happened less than five minutes ago', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Notification, {
        link: /*#__PURE__*/React.createElement(Link, {
          href: "notifications/1"
        }),
        name: "Thomas Stephens",
        action: "added a new comment to your",
        on: "review",
        when: new Date('2019-11-05T10:57:00.000Z'),
        description: "A long description that will be shortened"
      }));
    });
    it('should render the notification as a list item', function () {
      var listItem = wrapper.container.firstElementChild;
      expect(listItem === null || listItem === void 0 ? void 0 : listItem.tagName).toEqual('LI');
    });
    it('should set the href of the entire notification item', function () {
      var listItem = wrapper.container.firstElementChild;
      var itemWrapper = listItem === null || listItem === void 0 ? void 0 : listItem.firstElementChild;
      var link = itemWrapper === null || itemWrapper === void 0 ? void 0 : itemWrapper.firstElementChild;
      expect(link === null || link === void 0 ? void 0 : link.getAttribute('href')).toEqual('notifications/1');
    });
    it('should render the initials as an avatar', function () {
      expect(wrapper.getByText('TS')).toBeTruthy();
    });
    it('adds the aria-hidden attribute to the initials', function () {
      expect(wrapper.getByText('TS')).toHaveAttribute('aria-hidden');
    });
    it('should render the not-read indicator', function () {
      expect(wrapper.getByTestId('not-read-item')).toBeTruthy();
    });
    it('should render the name', function () {
      expect(wrapper.getByText('Thomas Stephens')).toBeTruthy();
    });
    it('should render the action', function () {
      expect(wrapper.getByText('added a new comment to your')).toBeTruthy();
    });
    it('should render the on value', function () {
      expect(wrapper.getByText('review')).toBeTruthy();
    });
    it('should render the circle', function () {
      expect(wrapper.getByTestId('circle')).toBeTruthy();
    });
    it('should render the when as "Just now"', function () {
      expect(wrapper.getByText('Just now')).toBeTruthy();
    });
    it('should render the shortened description', function () {
      expect(wrapper.getByText('A long description that will be shorte...')).toBeTruthy();
    });
  });
  describe('when the notification happened an hour ago', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Notification, {
        link: /*#__PURE__*/React.createElement(Link, {
          href: "notifications/1"
        }),
        name: "Thomas Stephens",
        action: "added a new comment to your",
        on: "review",
        when: new Date('2019-11-05T09:57:00.000Z'),
        description: "A long description that will be shortened"
      }));
    });
    it('should render the when as "1 hour ago"', function () {
      expect(wrapper.getByText('1 hour ago')).toBeTruthy();
    });
  });
  describe('when the notification happened over 24 hours ago', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Notification, {
        link: /*#__PURE__*/React.createElement(Link, {
          href: "notifications/1"
        }),
        name: "Thomas Stephens",
        action: "added a new comment to your",
        on: "review",
        when: new Date('2019-11-04T09:57:00.000Z'),
        description: "A long description that will be shortened"
      }));
    });
    it('should render the when as "dd MMM yyyy"', function () {
      expect(wrapper.getByText('04 Nov 2019')).toBeTruthy();
    });
  });
  describe('when the name has a double barrelled surname', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Notification, {
        link: /*#__PURE__*/React.createElement(Link, {
          href: "notifications/1"
        }),
        name: "Thomas Double-Barrelled",
        action: "added a new comment to your",
        on: "review",
        when: new Date('2019-11-05T10:57:00.000Z'),
        description: "A long description that will be shortened"
      }));
    });
    it('should render the initials as an avatar', function () {
      expect(wrapper.getByText('TDB')).toBeTruthy();
    });
  });
  describe('when the notification has been read', function () {
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(Notification, {
        link: /*#__PURE__*/React.createElement(Link, {
          href: "notifications/1"
        }),
        name: "Thomas Stephens",
        isRead: true,
        action: "added a new comment to your",
        on: "review",
        when: new Date('2019-11-05T10:57:00.000Z'),
        description: "A long description that will be shortened"
      }));
    });
    it('should not render the not-read indicator', function () {
      expect(wrapper.queryAllByTestId('not-read')).toHaveLength(0);
    });
  });
  describe('when isRead changes', function () {
    var ExampleNotification = function ExampleNotification(_ref) {
      var isRead = _ref.isRead;
      return /*#__PURE__*/React.createElement(Notification, {
        link: /*#__PURE__*/React.createElement(Link, {
          href: "notifications/1"
        }),
        name: "Thomas Stephens",
        action: "added a new comment to your",
        on: "review",
        when: new Date('2019-11-05T10:57:00.000Z'),
        description: "description",
        isRead: isRead
      });
    };
    var initialId;
    beforeEach(function () {
      wrapper = render( /*#__PURE__*/React.createElement(ExampleNotification, null));
      initialId = wrapper.getByTestId('notification-content').id;
      wrapper.rerender( /*#__PURE__*/React.createElement(ExampleNotification, {
        isRead: true
      }));
    });
    it('does not generate a new content `id`', function () {
      expect(wrapper.getByTestId('notification-content')).toHaveAttribute('id', initialId);
    });
  });
});