Untitled

 avatar
unknown
plain_text
13 days ago
3.8 kB
6
Indexable
import { renderHook, act } from '@testing-library/react-hooks';
import { useTrainingCourseDetail } from '../useTrainingCourseDetail';
import * as Sentry from '@sentry/react-native';
import { useToast } from '@spark-mobile/toast';
import { useNavigation } from '@react-navigation/native';
import { EnrollmentStatus } from '@app-types/TrainingCourses';

jest.mock('@sentry/react-native');
jest.mock('@spark-mobile/toast');
jest.mock('@react-navigation/native');
jest.mock('@components/hooks/useCreateTrainingEnrollmentMutation', () => ({
  useCreateTrainingEnrollmentMutation: () => [jest.fn(), {}],
}));
jest.mock('@components/hooks/useSubmitTrainingEnrollmentMutation', () => ({
  useSubmitTrainingEnrollmentMutation: () => [jest.fn()],
}));
jest.mock('@components/hooks/useGetPresignedUrlData', () => ({
  useGetPresignedUrlData: jest.fn(() => ({})),
}));
jest.mock('@components/hooks/useGetTrainingCourseData', () => ({
  useGetTrainingCourseData: () => ({
    data: {
      course: {
        enrollments: [{ id: 'enroll123', status: EnrollmentStatus.InProgress }],
      },
    },
  }),
}));
jest.mock('@core/i18n', () => ({
  translate: () => 'Something went wrong',
}));

const mockAddToast = jest.fn();
const mockGoBack = jest.fn();

beforeEach(() => {
  (useToast as jest.Mock).mockReturnValue({ addToast: mockAddToast });
  (useNavigation as jest.Mock).mockReturnValue({ goBack: mockGoBack });
  jest.clearAllMocks();
});

describe('useTrainingCourseDetail', () => {
  it('should handle course completion with enrollment id', async () => {
    const { result } = renderHook(() => useTrainingCourseDetail({ courseId: '123' }));
    const messageEvent = {
      nativeEvent: {
        data: JSON.stringify(JSON.stringify({ type: 'course', event: 'complete' })),
      },
    };

    const mockSubmitTrainingEnrollment = require('@components/hooks/useSubmitTrainingEnrollmentMutation')
      .useSubmitTrainingEnrollmentMutation()[0];

    await act(() => {
      result.current.handleMessage(messageEvent as any);
    });

    expect(mockSubmitTrainingEnrollment).toHaveBeenCalledWith({
      variables: { input: { id: 'enroll123' } },
    });
  });

  it('should not submit if course is already completed', async () => {
    // Mock completed status
    jest.mock('@components/hooks/useGetTrainingCourseData', () => ({
      useGetTrainingCourseData: () => ({
        data: {
          course: {
            enrollments: [{ id: 'enroll123', status: EnrollmentStatus.Completed }],
          },
        },
      }),
    }));

    const { result } = renderHook(() => useTrainingCourseDetail({ courseId: '123' }));
    const messageEvent = {
      nativeEvent: {
        data: JSON.stringify(JSON.stringify({ type: 'course', event: 'complete' })),
      },
    };

    const mockSubmitTrainingEnrollment = require('@components/hooks/useSubmitTrainingEnrollmentMutation')
      .useSubmitTrainingEnrollmentMutation()[0];

    await act(() => {
      result.current.handleMessage(messageEvent as any);
    });

    expect(mockSubmitTrainingEnrollment).not.toHaveBeenCalled();
  });

  it('should handle malformed message and call error handler', async () => {
    const { result } = renderHook(() => useTrainingCourseDetail({ courseId: '123' }));
    const badMessageEvent = {
      nativeEvent: { data: 'invalid-json' },
    };

    await act(() => {
      result.current.handleMessage(badMessageEvent as any);
    });

    expect(mockAddToast).toHaveBeenCalledWith({
      tone: 'critical',
      message: 'Something went wrong',
    });
    expect(Sentry.captureException).toHaveBeenCalled();
    expect(mockGoBack).toHaveBeenCalled();
  });
});
Editor is loading...
Leave a Comment