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.

App-test.tsx 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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. /* eslint-disable import/first, import/order */
  21. jest.mock('../../../../api/measures', () => ({
  22. getMeasures: jest.fn(() => Promise.resolve([]))
  23. }));
  24. jest.mock('../../../../api/components', () => ({
  25. getChildren: jest.fn(() => Promise.resolve({ components: [], paging: { total: 0 } }))
  26. }));
  27. // mock Activity to not deal with localstorage
  28. jest.mock('../Activity', () => ({
  29. // eslint-disable-next-line
  30. default: function Activity() {
  31. return null;
  32. }
  33. }));
  34. jest.mock('../Report', () => ({
  35. // eslint-disable-next-line
  36. default: function Report() {
  37. return null;
  38. }
  39. }));
  40. import * as React from 'react';
  41. import { shallow, mount } from 'enzyme';
  42. import { App } from '../App';
  43. import { Component } from '../../../../app/types';
  44. const getMeasures = require('../../../../api/measures').getMeasures as jest.Mock<any>;
  45. const getChildren = require('../../../../api/components').getChildren as jest.Mock<any>;
  46. const component = { key: 'foo', name: 'Foo', qualifier: 'TRK' } as Component;
  47. it('renders', () => {
  48. const wrapper = shallow(<App component={component} fetchMetrics={jest.fn()} metrics={{}} />);
  49. wrapper.setState({
  50. loading: false,
  51. measures: { ncloc: '173', reliability_rating: '1' },
  52. subComponents: [],
  53. totalSubComponents: 0
  54. });
  55. expect(wrapper).toMatchSnapshot();
  56. });
  57. it('renders when portfolio is empty', () => {
  58. const wrapper = shallow(<App component={component} fetchMetrics={jest.fn()} metrics={{}} />);
  59. wrapper.setState({ loading: false, measures: { reliability_rating: '1' } });
  60. expect(wrapper).toMatchSnapshot();
  61. });
  62. it('renders when portfolio is not computed', () => {
  63. const wrapper = shallow(<App component={component} fetchMetrics={jest.fn()} metrics={{}} />);
  64. wrapper.setState({ loading: false, measures: { ncloc: '173' } });
  65. expect(wrapper).toMatchSnapshot();
  66. });
  67. it('fetches measures and children components', () => {
  68. getMeasures.mockClear();
  69. getChildren.mockClear();
  70. mount(<App component={component} fetchMetrics={jest.fn()} metrics={{}} />);
  71. expect(getMeasures).toBeCalledWith({
  72. componentKey: 'foo',
  73. metricKeys:
  74. 'projects,ncloc,ncloc_language_distribution,releasability_rating,releasability_effort,sqale_rating,maintainability_rating_effort,reliability_rating,reliability_rating_effort,security_rating,security_rating_effort,last_change_on_releasability_rating,last_change_on_maintainability_rating,last_change_on_security_rating,last_change_on_reliability_rating'
  75. });
  76. expect(getChildren).toBeCalledWith(
  77. 'foo',
  78. [
  79. 'ncloc',
  80. 'releasability_rating',
  81. 'security_rating',
  82. 'reliability_rating',
  83. 'sqale_rating',
  84. 'alert_status'
  85. ],
  86. { ps: 20, s: 'qualifier' }
  87. );
  88. });