BookDo7starsTS / bookdo7stars_be

0 stars 0 forks source link

test/job/aladinBooks.test.js Practiced #10

Closed MayHyeyeonKim closed 2 months ago

MayHyeyeonKim commented 2 months ago

cho cho cho dae bak difficult!... I can't live without ChatGPT anymore. Please take a look at this and let me know.

import * as SaveAladinBooks from '../../src/job/SaveAladinBooks';

jest.mock('../../src/job/SaveAladinBooks', () => {
  const actualModule = jest.requireActual('../../src/job/SaveAladinBooks');

  return {
    ...actualModule,
    getAladinBooks: jest.fn(), // 모의화 추가
    getAladinBooksCountByQueryType: jest.fn(),
    fetchAladinBooksByQueryType: jest.fn(),
  };
});

describe('getAladinBooks', () => {
  beforeEach(() => {
    jest.useFakeTimers();
    jest.clearAllMocks();
  });

  jest.setTimeout(10000);

  it('should fetch books from Aladin API the correct number of times', async () => {
    SaveAladinBooks.getAladinBooksCountByQueryType.mockResolvedValue(1000); // 총 1000건의 책이 있다고 가정
    SaveAladinBooks.fetchAladinBooksByQueryType.mockResolvedValue([]); // 빈 배열 반환

    // 모의화된 getAladinBooks 함수의 동작을 구현합니다.
    SaveAladinBooks.getAladinBooks.mockImplementation(async (queryType) => {
      const totalCount = await SaveAladinBooks.getAladinBooksCountByQueryType(queryType);
      for (let i = 1; i <= Math.ceil(totalCount / 50); i++) {
        await SaveAladinBooks.fetchAladinBooksByQueryType(queryType, i);
      }
      return true;
    });

    const queryType = 'ItemNewAll';
    const result = await SaveAladinBooks.getAladinBooks(queryType);

    console.log('result:', result); // 반환 값 확인

    expect(SaveAladinBooks.getAladinBooksCountByQueryType).toHaveBeenCalledTimes(1);
    expect(SaveAladinBooks.getAladinBooksCountByQueryType).toHaveBeenCalledWith(queryType);
    expect(SaveAladinBooks.fetchAladinBooksByQueryType).toHaveBeenCalledTimes(20); // 1000/50 = 20 페이지
    expect(SaveAladinBooks.fetchAladinBooksByQueryType).toHaveBeenCalledWith(queryType, 1);
    expect(SaveAladinBooks.fetchAladinBooksByQueryType).toHaveBeenCalledWith(queryType, 2);
    expect(SaveAladinBooks.fetchAladinBooksByQueryType).toHaveBeenCalledWith(queryType, 3);
  });
});
Joel-Hwang commented 2 months ago

요거 works fine. import aladinBooksJob from '../../src/job/SaveAladinBooks';

describe('getAladinBooks', () => { beforeEach(() => { jest.clearAllMocks(); });

it('should fetch books from Aladin API the correct number of times', async () => { jest.spyOn(aladinBooksJob, 'getAladinBooksCountByQueryType').mockImplementation(() => 987); jest.spyOn(aladinBooksJob, 'fetchAladinBooksByQueryType').mockImplementation(() => []);

// getAladinBooks 함수 호출
await aladinBooksJob.getAladinBooks('ItemNewAll');

// getAladinBooksCountByQueryType가 정확한 queryType으로 호출되었는지 확인
expect(aladinBooksJob.getAladinBooksCountByQueryType).toHaveBeenCalledWith('ItemNewAll');

// 총 100개의 도서, 페이지당 50개이므로 fetchAladinBooksByQueryType이 2번 호출되어야 함
expect(aladinBooksJob.fetchAladinBooksByQueryType).toHaveBeenCalledTimes(20);

}); });