search.spec.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { ActionKeyPressed, ActionTypeKeyPressed, Keys } from '../../../../hooks/vim';
  2. import { Song } from '../../../../types';
  3. import { CmusUIActionType, searched } from '../actions';
  4. import { CmusUIState, LibraryModeWindow, View } from '../types';
  5. import { stateSearching } from './fixtures';
  6. import { cmusUIReducer, initialCmusUIState } from './reducer';
  7. describe('Searching', () => {
  8. const actionToSearch: ActionKeyPressed = { type: ActionTypeKeyPressed, key: Keys.slash };
  9. describe(Keys.slash, () => {
  10. it('should set searchMode to true', () => {
  11. expect.assertions(1);
  12. const result = cmusUIReducer(initialCmusUIState, actionToSearch);
  13. expect(result.searchMode).toBe(true);
  14. });
  15. });
  16. describe(CmusUIActionType.Searched, () => {
  17. const stateSearchingArtists: CmusUIState = {
  18. ...stateSearching,
  19. view: View.Library,
  20. artists: ['Amy Winehouse', 'Anticon', 'Bach'],
  21. library: {
  22. ...stateSearching.library,
  23. activeArtist: 'Amy Winehouse',
  24. activeAlbum: 'Back to Black',
  25. modeWindow: LibraryModeWindow.ArtistList,
  26. activeSongId: 883,
  27. },
  28. };
  29. const stateSearchingSongs: CmusUIState = {
  30. ...stateSearching,
  31. view: View.Library,
  32. artists: ['Amy Winehouse'],
  33. artistSongs: {
  34. 'Amy Winehouse': [
  35. { id: 184, title: 'Rehab' } as Song,
  36. { id: 883, title: 'Wake Up Alone' } as Song,
  37. ],
  38. },
  39. library: {
  40. ...stateSearching.library,
  41. activeArtist: 'Amy Winehouse',
  42. activeSongId: null,
  43. modeWindow: LibraryModeWindow.SongList,
  44. },
  45. };
  46. describe('artists', () => {
  47. it('should select the first match', () => {
  48. expect.assertions(3);
  49. const result = cmusUIReducer(stateSearchingArtists, searched('ant'));
  50. expect(result.library.activeArtist).toBe('Anticon');
  51. expect(result.library.activeSongId).toBeNull();
  52. expect(result.library.activeAlbum).toBeNull();
  53. });
  54. describe('when the artist has songs loaded', () => {
  55. const stateWithSongsLoaded: CmusUIState = {
  56. ...stateSearchingArtists,
  57. artistSongs: { Anticon: [{ id: 174 } as Song] },
  58. };
  59. it('should select the first song', () => {
  60. expect.assertions(1);
  61. const result = cmusUIReducer(stateWithSongsLoaded, searched('ant'));
  62. expect(result.library.activeSongId).toBe(174);
  63. });
  64. });
  65. });
  66. describe('songs', () => {
  67. it('should select the first match (by title)', () => {
  68. expect.assertions(1);
  69. const result = cmusUIReducer(stateSearchingSongs, searched('w'));
  70. expect(result.library.activeSongId).toBe(883);
  71. });
  72. });
  73. describe('when finishing / cancelling', () => {
  74. it('should set searchMode to false', () => {
  75. expect.assertions(1);
  76. const result = cmusUIReducer(stateSearchingArtists, searched(null));
  77. expect(result.searchMode).toBe(false);
  78. });
  79. });
  80. });
  81. });