File size: 6,101 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
/**
* @jest-environment jsdom
*/
import { renderHook, act } from '@testing-library/react';
import { translate } from 'i18n-calypso';
import { useSelector, useDispatch } from 'react-redux';
import { getCurrentUserName } from 'calypso/state/current-user/selectors';
import {
addRecommendedBlogsSite,
removeRecommendedBlogsSite,
} from 'calypso/state/reader/lists/actions';
import { getListByOwnerAndSlug, getMatchingItem } from 'calypso/state/reader/lists/selectors';
import { useFeedRecommendationsMutation } from '..';
import type { AppState } from 'calypso/types';
// Mock dependencies
jest.mock( 'react-redux' );
jest.mock( 'i18n-calypso' );
jest.mock( 'calypso/state/current-user/selectors' );
jest.mock( 'calypso/state/reader/lists/actions' );
jest.mock( 'calypso/state/reader/lists/selectors' );
const mockUseSelector = useSelector as jest.MockedFunction< typeof useSelector >;
const mockUseDispatch = useDispatch as jest.MockedFunction< typeof useDispatch >;
const mockGetCurrentUserName = getCurrentUserName as jest.MockedFunction<
typeof getCurrentUserName
>;
const mockGetListByOwnerAndSlug = getListByOwnerAndSlug as jest.MockedFunction<
typeof getListByOwnerAndSlug
>;
const mockGetMatchingItem = getMatchingItem as jest.MockedFunction< typeof getMatchingItem >;
const mockTranslate = translate as jest.MockedFunction< typeof translate >;
describe( 'useFeedRecommendationsMutation', () => {
const mockDispatch = jest.fn();
const mockFeedId = 123;
const mockCurrentUser = 'testuser';
const mockList = { ID: 456, owner: 'testuser', slug: 'recommended-blogs' };
beforeEach( () => {
jest.clearAllMocks();
mockUseDispatch.mockReturnValue( mockDispatch );
mockTranslate.mockImplementation( ( text ) => text );
// Mock selector functions directly
mockGetCurrentUserName.mockReturnValue( mockCurrentUser );
mockGetListByOwnerAndSlug.mockReturnValue( mockList );
mockGetMatchingItem.mockReturnValue( false ); // Default: not recommended
// Mock useSelector to call the actual selector functions
mockUseSelector.mockImplementation( ( selector ) => {
// Call the selector with a mock state - the selector will use our mocked functions
return selector( {} as AppState );
} );
} );
describe( 'Basic functionality', () => {
it( 'should return correct initial state', () => {
const { result } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
expect( result.current ).toEqual( {
isRecommended: false,
isUpdating: false,
canToggle: true,
toggleRecommended: expect.any( Function ),
} );
} );
it( 'should return true for isRecommended when feed is in list', () => {
// Mock that the feed is found in the list
mockGetMatchingItem.mockReturnValue( { feed_ID: mockFeedId } );
const { result } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
expect( result.current.isRecommended ).toBe( true );
} );
it( 'should return false for canToggle when no user', () => {
mockGetCurrentUserName.mockReturnValue( null );
const { result } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
expect( result.current.canToggle ).toBe( false );
} );
it( 'should return false for canToggle when no list', () => {
mockGetListByOwnerAndSlug.mockReturnValue( undefined );
const { result } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
expect( result.current.canToggle ).toBe( false );
} );
} );
describe( 'Toggle functionality', () => {
it( 'should dispatch addRecommendedBlogsSite when toggling on', () => {
const { result } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
act( () => {
result.current.toggleRecommended();
} );
expect( mockDispatch ).toHaveBeenCalledWith(
addRecommendedBlogsSite( mockList.ID, mockFeedId, mockCurrentUser, {
successMessage: 'Site added to your recommended blogs.',
errorMessage: 'Failed to add site to recommended blogs. Please try again.',
} )
);
} );
it( 'should dispatch removeRecommendedBlogsSite when toggling off', () => {
// Mock that the site is already recommended
mockGetMatchingItem.mockReturnValue( { feed_ID: mockFeedId } );
const { result } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
act( () => {
result.current.toggleRecommended();
} );
expect( mockDispatch ).toHaveBeenCalledWith(
removeRecommendedBlogsSite( mockList.ID, mockFeedId, mockCurrentUser, {
successMessage: 'Site removed from your recommended blogs.',
errorMessage: 'Failed to remove site from recommended blogs.',
} )
);
} );
it( 'should not dispatch when canToggle is false', () => {
mockGetCurrentUserName.mockReturnValue( null );
const { result } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
act( () => {
result.current.toggleRecommended();
} );
expect( mockDispatch ).not.toHaveBeenCalled();
} );
} );
describe( 'Redux integration', () => {
it( 'should react to selector function changes', () => {
const { result, rerender } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
expect( result.current.isRecommended ).toBe( false );
// Change the mock return value to simulate Redux state change
mockGetMatchingItem.mockReturnValue( { feed_ID: mockFeedId } );
rerender();
expect( result.current.isRecommended ).toBe( true );
} );
it( 'should handle missing user gracefully', () => {
mockGetCurrentUserName.mockReturnValue( null );
const { result } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
expect( result.current.isRecommended ).toBe( false );
expect( result.current.canToggle ).toBe( false );
} );
it( 'should handle missing list gracefully', () => {
mockGetListByOwnerAndSlug.mockReturnValue( undefined );
const { result } = renderHook( () => useFeedRecommendationsMutation( mockFeedId ) );
expect( result.current.isRecommended ).toBe( false );
expect( result.current.canToggle ).toBe( false );
} );
} );
} );
|