11import { render , screen , fireEvent , waitFor } from '@testing-library/react' ;
22import { describe , it , expect } from '@jest/globals' ;
33import { URLShortener } from './URLShortener' ;
4- import React from 'react' ;
4+ import React , { act } from 'react' ;
5+
6+ global . fetch = jest . fn ( ) ;
57
68describe ( 'URLShortenerV2' , ( ) => {
9+ beforeEach ( ( ) => {
10+ jest . clearAllMocks ( ) ; // Reset all mocks before each test
11+ } ) ;
12+
713 it ( "renders the long URL input and Shorten button in 'initial' mode" , ( ) => {
814 render ( < URLShortener /> ) ;
915
@@ -17,6 +23,12 @@ describe('URLShortenerV2', () => {
1723 } ) ;
1824
1925 it ( "clicking the 'Shorten' button triggers the shorten action" , async ( ) => {
26+ const shortenedUrl = 'https://short.ly/abc123' ;
27+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
28+ ok : true ,
29+ json : async ( ) => ( { shortenedUrl } ) , // API response in JSON
30+ } ) ;
31+
2032 render ( < URLShortener /> ) ;
2133
2234 const enterUrlTextBox = screen . getByRole ( 'textbox' , {
@@ -39,7 +51,8 @@ describe('URLShortenerV2', () => {
3951 const shortenedUrlTextBox = screen . getByRole ( 'textbox' , {
4052 name : / s h o r t e n e d u r l / i,
4153 } ) ;
42- expect ( shortenedUrlTextBox ) . toHaveValue ( 'should-be-a-shortened-url' ) ;
54+ //expect(shortenedUrlTextBox).toHaveValue('should-be-a-shortened-url');
55+ expect ( shortenedUrlTextBox ) . toHaveValue ( shortenedUrl ) ;
4356 expect (
4457 screen . queryByRole ( 'button' , { name : / s h o r t e n u r l / i } )
4558 ) . not . toBeInTheDocument ( ) ;
0 commit comments