11import roomGacha from './index' ;
22import Slack from '../lib/slackMock' ;
3+ import axios from 'axios' ;
4+ import type { AxiosResponse } from 'axios' ;
5+ import { promises as fs } from 'fs' ;
36import { stripIndents } from 'common-tags' ;
47import assert from 'assert' ;
58import type { KnownBlock } from '@slack/web-api' ;
9+ import type { ScrapeOptions } from 'scrape-it' ;
610
7- vi . mock ( 'scrape-it' , async ( importOriginal ) => {
8- const actual = await importOriginal < typeof import ( 'scrape-it' ) > ( ) ;
9- const { scrapeHTML} = actual ;
11+ vi . mock ( 'axios' ) ;
12+
13+ // scrape-itからのaxiosの呼び出しはモックできないため、簡易実装を提供する
14+ vi . mock ( 'scrape-it' , async ( ) => {
15+ const scrapeIt = await vi . importActual < typeof import ( 'scrape-it' ) > ( 'scrape-it' ) ;
16+ const cheerio = await vi . importActual < typeof import ( 'cheerio' ) > ( 'cheerio' ) ;
1017 return {
11- default : vi . fn ( async ( _url : unknown , opts : Parameters < typeof scrapeHTML > [ 1 ] ) => {
12- const { promises : fs } = await import ( 'fs' ) ;
13- const html = await fs . readFile ( `${ __dirname } /search-result.test.html` , 'utf-8' ) ;
14- return { data : scrapeHTML ( html , opts ) } ;
15- } ) ,
16- scrapeHTML,
18+ default : async ( url : string , opts : ScrapeOptions ) => {
19+ const res = await axios ( url ) ;
20+ return { data : scrapeIt . scrapeHTML ( cheerio . load ( res . data ) , opts ) } ;
21+ } ,
1722 } ;
1823} ) ;
1924
@@ -27,6 +32,9 @@ beforeEach(async () => {
2732
2833describe ( 'room-gacha' , ( ) => {
2934 it ( 'responds to "物件ガチャ" with a prefecture and a city specified' , async ( ) => {
35+ const data = await fs . readFile ( `${ __dirname } /search-result.test.html` , 'utf-8' ) ;
36+ const mockAxios = vi . mocked ( axios ) ;
37+ mockAxios . mockResolvedValue ( { data} as AxiosResponse ) ;
3038 const response = await slack . getResponseTo ( '物件ガチャ 東京都 文京区' ) ;
3139 const blocks = 'blocks' in response ? response . blocks : [ ] ;
3240 expect ( 'username' in response && response . username ) . toBe ( '物件ガチャ' ) ;
0 commit comments