scroll.spec.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { getNextActiveArtistAndAlbum, getArtistAlbumScrollIndex } from './scroll';
  2. describe(getNextActiveArtistAndAlbum.name, () => {
  3. const artists: string[] = ['A', 'B', 'C'];
  4. const artistAlbums: Record<string, string[]> = {
  5. A: ['a1', 'a2', 'a3'],
  6. C: ['c1', 'c2'],
  7. };
  8. describe('scrolling down', () => {
  9. describe('when nothing is active', () => {
  10. it('switch to the first artist', () => {
  11. expect.assertions(1);
  12. expect(getNextActiveArtistAndAlbum(artists, artistAlbums, null, null, [], 1)).toStrictEqual(
  13. {
  14. artist: 'A',
  15. album: null,
  16. },
  17. );
  18. });
  19. });
  20. describe('when the first artist is active', () => {
  21. describe('when its albums are not expanded', () => {
  22. it('should switch to the second artist', () => {
  23. expect.assertions(1);
  24. expect(
  25. getNextActiveArtistAndAlbum(artists, artistAlbums, 'A', null, [], 1),
  26. ).toStrictEqual({
  27. artist: 'B',
  28. album: null,
  29. });
  30. });
  31. });
  32. describe('when its albums are expanded', () => {
  33. it('should switch to the first album', () => {
  34. expect.assertions(1);
  35. expect(
  36. getNextActiveArtistAndAlbum(artists, artistAlbums, 'A', null, ['A'], 1),
  37. ).toStrictEqual({
  38. artist: 'A',
  39. album: 'a1',
  40. });
  41. });
  42. });
  43. });
  44. describe('when the last artist is active', () => {
  45. describe('when its albums are expanded', () => {
  46. it.each`
  47. activeAlbum | nextAlbum
  48. ${null} | ${'c1'}
  49. ${'c1'} | ${'c2'}
  50. `('should switch to the next album ($nextAlbum)', ({ activeAlbum, nextAlbum }) => {
  51. expect(
  52. getNextActiveArtistAndAlbum(artists, artistAlbums, 'C', activeAlbum, ['C'], 1),
  53. ).toStrictEqual({
  54. artist: 'C',
  55. album: nextAlbum,
  56. });
  57. });
  58. describe('when the last album is active', () => {
  59. it('should keep the current selection', () => {
  60. expect.assertions(1);
  61. expect(
  62. getNextActiveArtistAndAlbum(artists, artistAlbums, 'C', 'c2', ['C'], 1),
  63. ).toStrictEqual({
  64. artist: 'C',
  65. album: 'c2',
  66. });
  67. });
  68. });
  69. });
  70. describe('when its albums are not expanded', () => {
  71. it('should keep the current selection', () => {
  72. expect.assertions(1);
  73. expect(
  74. getNextActiveArtistAndAlbum(artists, artistAlbums, 'C', null, ['A'], 1),
  75. ).toStrictEqual({
  76. artist: 'C',
  77. album: null,
  78. });
  79. });
  80. });
  81. });
  82. describe('when the second-to-last artist is active', () => {
  83. describe('when the last album is active', () => {
  84. it('should select the last artist', () => {
  85. expect.assertions(1);
  86. const artistAlbums2 = {
  87. B: ['b1', 'b2', 'b3'],
  88. };
  89. expect(
  90. getNextActiveArtistAndAlbum(artists, artistAlbums2, 'B', 'b3', ['B'], 1),
  91. ).toStrictEqual({
  92. artist: 'C',
  93. album: null,
  94. });
  95. });
  96. });
  97. });
  98. });
  99. describe('scrolling up', () => {
  100. describe('when nothing is active', () => {
  101. it('should switch to the last artist', () => {
  102. expect.assertions(1);
  103. expect(
  104. getNextActiveArtistAndAlbum(artists, artistAlbums, null, null, [], -1),
  105. ).toStrictEqual({
  106. artist: 'C',
  107. album: null,
  108. });
  109. });
  110. describe('when the last albums are expanded', () => {
  111. it('should switch to the last album', () => {
  112. expect.assertions(1);
  113. expect(
  114. getNextActiveArtistAndAlbum(artists, artistAlbums, null, null, ['C'], -1),
  115. ).toStrictEqual({
  116. artist: 'C',
  117. album: 'c2',
  118. });
  119. });
  120. });
  121. });
  122. describe('when the first artist is active', () => {
  123. describe('when its albums are not expanded', () => {
  124. it('should keep the current selection', () => {
  125. expect.assertions(1);
  126. expect(
  127. getNextActiveArtistAndAlbum(artists, artistAlbums, 'A', null, [], -1),
  128. ).toStrictEqual({
  129. artist: 'A',
  130. album: null,
  131. });
  132. });
  133. });
  134. describe('when its albums are expanded', () => {
  135. it('should switch to the previous album', () => {
  136. expect.assertions(1);
  137. expect(
  138. getNextActiveArtistAndAlbum(artists, artistAlbums, 'A', 'a3', ['A'], -1),
  139. ).toStrictEqual({
  140. artist: 'A',
  141. album: 'a2',
  142. });
  143. });
  144. describe('when the first album is active', () => {
  145. it('should switch to the artist', () => {
  146. expect.assertions(1);
  147. expect(
  148. getNextActiveArtistAndAlbum(artists, artistAlbums, 'A', 'a1', ['A'], -1),
  149. ).toStrictEqual({
  150. artist: 'A',
  151. album: null,
  152. });
  153. });
  154. });
  155. describe('when no album is active', () => {
  156. it('should keep the current selection', () => {
  157. expect.assertions(1);
  158. expect(
  159. getNextActiveArtistAndAlbum(artists, artistAlbums, 'A', null, ['A'], -1),
  160. ).toStrictEqual({
  161. artist: 'A',
  162. album: null,
  163. });
  164. });
  165. });
  166. });
  167. });
  168. describe('when a middle or last artist is active', () => {
  169. describe('when no album is active', () => {
  170. describe('when the previous albums are expanded', () => {
  171. it('should switch to the last album of the previous artist', () => {
  172. expect.assertions(1);
  173. expect(
  174. getNextActiveArtistAndAlbum(artists, artistAlbums, 'B', null, ['A'], -1),
  175. ).toStrictEqual({
  176. artist: 'A',
  177. album: 'a3',
  178. });
  179. });
  180. });
  181. describe('when the previous albums are not expanded', () => {
  182. it('should switch to the previous artist', () => {
  183. expect.assertions(1);
  184. expect(
  185. getNextActiveArtistAndAlbum(artists, artistAlbums, 'B', null, [], -1),
  186. ).toStrictEqual({
  187. artist: 'A',
  188. album: null,
  189. });
  190. });
  191. });
  192. });
  193. describe('when an album is active', () => {
  194. it('should switch to the previous album', () => {
  195. expect.assertions(1);
  196. expect(
  197. getNextActiveArtistAndAlbum(artists, artistAlbums, 'C', 'c2', ['C'], -1),
  198. ).toStrictEqual({
  199. artist: 'C',
  200. album: 'c1',
  201. });
  202. });
  203. });
  204. });
  205. });
  206. });
  207. describe(getArtistAlbumScrollIndex.name, () => {
  208. describe('when on the Nth artist', () => {
  209. it('should return N', () => {
  210. expect.assertions(3);
  211. expect(getArtistAlbumScrollIndex(['A', 'B', 'C'], {}, 'A', null, [])).toBe(0);
  212. expect(getArtistAlbumScrollIndex(['A', 'B', 'C'], {}, 'B', null, [])).toBe(1);
  213. expect(getArtistAlbumScrollIndex(['A', 'B', 'C'], {}, 'C', null, [])).toBe(2);
  214. });
  215. });
  216. describe('when an artist has its albums expanded', () => {
  217. it('should return the correct row number', () => {
  218. expect.assertions(5);
  219. const artistAlbums: Record<string, string[]> = {
  220. A: ['a1', 'a2'],
  221. B: ['b1'],
  222. };
  223. expect(getArtistAlbumScrollIndex(['A', 'B'], artistAlbums, 'A', 'a1', ['A'])).toBe(1);
  224. expect(getArtistAlbumScrollIndex(['A', 'B'], artistAlbums, 'A', 'a2', ['A'])).toBe(2);
  225. expect(getArtistAlbumScrollIndex(['A', 'B'], artistAlbums, 'B', null, ['A'])).toBe(3);
  226. expect(getArtistAlbumScrollIndex(['A', 'B'], artistAlbums, 'B', 'b1', ['B'])).toBe(2);
  227. expect(getArtistAlbumScrollIndex(['A', 'B'], artistAlbums, 'B', 'b1', ['A', 'B'])).toBe(4);
  228. });
  229. });
  230. });