selectors.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { ApiKeysState } from 'app/types';
  2. import { getMultipleMockKeys } from '../__mocks__/apiKeysMock';
  3. import { getApiKeys, getApiKeysCount, getIncludeExpired, getIncludeExpiredDisabled } from './selectors';
  4. describe('API Keys selectors', () => {
  5. const mockKeys = getMultipleMockKeys(5);
  6. const mockKeysIncludingExpired = getMultipleMockKeys(8);
  7. describe('getApiKeysCount', () => {
  8. it('returns the correct count when includeExpired is false', () => {
  9. const mockState: ApiKeysState = {
  10. keys: mockKeys,
  11. keysIncludingExpired: mockKeysIncludingExpired,
  12. searchQuery: '',
  13. hasFetched: true,
  14. includeExpired: false,
  15. };
  16. const keyCount = getApiKeysCount(mockState);
  17. expect(keyCount).toBe(5);
  18. });
  19. it('returns the correct count when includeExpired is true', () => {
  20. const mockState: ApiKeysState = {
  21. keys: mockKeys,
  22. keysIncludingExpired: mockKeysIncludingExpired,
  23. searchQuery: '',
  24. hasFetched: true,
  25. includeExpired: true,
  26. };
  27. const keyCount = getApiKeysCount(mockState);
  28. expect(keyCount).toBe(8);
  29. });
  30. });
  31. describe('getApiKeys', () => {
  32. describe('when includeExpired is false', () => {
  33. it('should return all keys if no search query', () => {
  34. const mockState: ApiKeysState = {
  35. keys: mockKeys,
  36. keysIncludingExpired: mockKeysIncludingExpired,
  37. searchQuery: '',
  38. hasFetched: true,
  39. includeExpired: false,
  40. };
  41. const keys = getApiKeys(mockState);
  42. expect(keys).toEqual(mockKeys);
  43. });
  44. it('should filter keys if search query exists', () => {
  45. const mockState: ApiKeysState = {
  46. keys: mockKeys,
  47. keysIncludingExpired: mockKeysIncludingExpired,
  48. searchQuery: '5',
  49. hasFetched: true,
  50. includeExpired: false,
  51. };
  52. const keys = getApiKeys(mockState);
  53. expect(keys.length).toEqual(1);
  54. });
  55. });
  56. describe('when includeExpired is true', () => {
  57. it('should return all keys if no search query', () => {
  58. const mockState: ApiKeysState = {
  59. keys: mockKeys,
  60. keysIncludingExpired: mockKeysIncludingExpired,
  61. searchQuery: '',
  62. hasFetched: true,
  63. includeExpired: true,
  64. };
  65. const keys = getApiKeys(mockState);
  66. expect(keys).toEqual(mockKeysIncludingExpired);
  67. });
  68. it('should filter keys if search query exists', () => {
  69. const mockState: ApiKeysState = {
  70. keys: mockKeys,
  71. keysIncludingExpired: mockKeysIncludingExpired,
  72. searchQuery: '5',
  73. hasFetched: true,
  74. includeExpired: true,
  75. };
  76. const keys = getApiKeys(mockState);
  77. expect(keys.length).toEqual(1);
  78. });
  79. });
  80. });
  81. describe('getIncludeExpired', () => {
  82. it('returns true if includeExpired is true', () => {
  83. const mockState: ApiKeysState = {
  84. keys: mockKeys,
  85. keysIncludingExpired: mockKeysIncludingExpired,
  86. searchQuery: '',
  87. hasFetched: true,
  88. includeExpired: true,
  89. };
  90. const includeExpired = getIncludeExpired(mockState);
  91. expect(includeExpired).toBe(true);
  92. });
  93. it('returns false if includeExpired is false', () => {
  94. const mockState: ApiKeysState = {
  95. keys: mockKeys,
  96. keysIncludingExpired: mockKeysIncludingExpired,
  97. searchQuery: '',
  98. hasFetched: true,
  99. includeExpired: false,
  100. };
  101. const includeExpired = getIncludeExpired(mockState);
  102. expect(includeExpired).toBe(false);
  103. });
  104. });
  105. describe('getIncludeExpiredDisabled', () => {
  106. it('returns true if there are no active keys but there are expired keys', () => {
  107. const mockState: ApiKeysState = {
  108. keys: [],
  109. keysIncludingExpired: mockKeysIncludingExpired,
  110. searchQuery: '',
  111. hasFetched: true,
  112. includeExpired: true,
  113. };
  114. const includeExpiredDisabled = getIncludeExpiredDisabled(mockState);
  115. expect(includeExpiredDisabled).toBe(true);
  116. });
  117. it('returns false otherwise', () => {
  118. const mockState: ApiKeysState = {
  119. keys: mockKeys,
  120. keysIncludingExpired: mockKeysIncludingExpired,
  121. searchQuery: '',
  122. hasFetched: true,
  123. includeExpired: false,
  124. };
  125. const includeExpiredDisabled = getIncludeExpired(mockState);
  126. expect(includeExpiredDisabled).toBe(false);
  127. });
  128. });
  129. });