You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NotificationsSidebar-test.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { shallow } from 'enzyme';
  21. import * as React from 'react';
  22. import { parseDate } from 'sonar-ui-common/helpers/dates';
  23. import NotificationsSidebar, {
  24. Feature,
  25. isUnread,
  26. Notification,
  27. Props
  28. } from '../NotificationsSidebar';
  29. const news: Props['news'] = [
  30. {
  31. notification: '10 Java rules, Github checks, Security Hotspots, BitBucket branch decoration',
  32. publicationDate: '2018-04-06',
  33. features: [
  34. {
  35. categories: [
  36. { color: '#ff0000', name: 'Java' },
  37. { color: '#00ff00', name: 'Rules' }
  38. ],
  39. description: '10 new Java rules'
  40. },
  41. {
  42. categories: [{ color: '#0000ff', name: 'BitBucket' }],
  43. description: 'BitBucket branch decoration',
  44. readMore: 'http://example.com'
  45. }
  46. ]
  47. },
  48. {
  49. notification: 'Some other notification',
  50. publicationDate: '2018-04-05',
  51. features: [
  52. {
  53. categories: [{ color: '#0000ff', name: 'BitBucket' }],
  54. description: 'BitBucket branch decoration',
  55. readMore: 'http://example.com'
  56. }
  57. ]
  58. }
  59. ];
  60. describe('#NotificationSidebar', () => {
  61. it('should render correctly if there are new features', () => {
  62. const wrapper = shallowRender({ loading: true });
  63. expect(wrapper).toMatchSnapshot();
  64. wrapper.setProps({ loading: false });
  65. expect(wrapper).toMatchSnapshot();
  66. expect(wrapper.find('Notification')).toHaveLength(2);
  67. });
  68. it('should render correctly if there are no new unread features', () => {
  69. const wrapper = shallowRender({
  70. notificationsLastReadDate: parseDate('2018-12-31')
  71. });
  72. expect(wrapper.find('Notification')).toHaveLength(2);
  73. expect(wrapper.find('Notification[unread=true]')).toHaveLength(0);
  74. });
  75. });
  76. describe('#isUnread', () => {
  77. it('should be unread', () => {
  78. expect(isUnread(0, '2018-12-14', undefined)).toBe(true);
  79. expect(isUnread(1, '2018-12-14', parseDate('2018-12-12'))).toBe(true);
  80. });
  81. it('should be read', () => {
  82. expect(isUnread(0, '2018-12-16', parseDate('2018-12-16'))).toBe(false);
  83. expect(isUnread(1, '2018-12-15', undefined)).toBe(false);
  84. });
  85. });
  86. describe('#Notification', () => {
  87. it('should render correctly', () => {
  88. expect(shallow(<Notification notification={news[1]} unread={false} />)).toMatchSnapshot();
  89. expect(shallow(<Notification notification={news[1]} unread={true} />)).toMatchSnapshot();
  90. });
  91. });
  92. describe('#Feature', () => {
  93. it('should render correctly', () => {
  94. expect(shallow(<Feature feature={news[1].features[0]} />)).toMatchSnapshot();
  95. expect(shallow(<Feature feature={news[0].features[0]} />)).toMatchSnapshot();
  96. });
  97. });
  98. function shallowRender(props: Partial<Props> = {}) {
  99. return shallow(
  100. <NotificationsSidebar
  101. fetchMoreFeatureNews={jest.fn()}
  102. loading={false}
  103. loadingMore={false}
  104. news={news}
  105. notificationsLastReadDate={parseDate('2018-01-01')}
  106. onClose={jest.fn()}
  107. paging={{ pageIndex: 1, pageSize: 10, total: 20 }}
  108. {...props}
  109. />
  110. );
  111. }