13
13
import fs from 'fs' ;
14
14
import { expect , use } from 'chai' ;
15
15
import sinon from 'sinon' ;
16
+ import sinonChai from 'sinon-chai' ;
16
17
import chaiAsPromised from 'chai-as-promised' ;
17
18
import * as downloadImagesModule from '../../src/aem/downloadImages.js' ;
18
19
import { downloadImagesInMarkdown } from '../../src/aem/downloadImages.js' ;
19
20
import { Readable , Writable } from 'stream' ;
20
21
import path from 'path' ;
21
22
import { expectLogContains } from '../utils.js' ;
22
23
24
+ use ( sinonChai ) ; // chai.use
23
25
use ( chaiAsPromised ) ;
24
26
25
27
describe ( 'downloadImages.js' , function ( ) {
@@ -119,14 +121,14 @@ describe('downloadImages.js', function () {
119
121
mkdirSyncStub . returns ( ) ;
120
122
121
123
downloadImagesModule . ensureDirSync ( 'path/to/directory' ) ;
122
- expect ( mkdirSyncStub . calledWith ( 'path/to/directory' , { recursive : true } ) ) . to . equal ( true ) ;
124
+ expect ( mkdirSyncStub ) . to . have . been . calledWith ( 'path/to/directory' , { recursive : true } ) ;
123
125
} ) ;
124
126
125
127
it ( 'should log error if directory creation fails' , ( ) => {
126
128
mkdirSyncStub . throws ( new Error ( 'Error creating directory' ) ) ;
127
129
128
130
downloadImagesModule . ensureDirSync ( 'path/to/directory' ) ;
129
- expect ( consoleErrorStub . calledWith ( sinon . match . string ) ) . to . equal ( true ) ;
131
+ expect ( consoleErrorStub ) . to . have . been . calledWith ( sinon . match . string ) ;
130
132
} ) ;
131
133
} ) ;
132
134
@@ -138,10 +140,10 @@ describe('downloadImages.js', function () {
138
140
'/content/dam/image.jpg' ,
139
141
) ;
140
142
141
- expect ( fetchStub . calledWith ( 'http://example.com/image.jpg' ) ) . to . equal ( true ) ;
143
+ expect ( fetchStub ) . to . have . been . calledWith ( 'http://example.com/image.jpg' ) ;
142
144
143
145
const finalPath = path . join ( process . cwd ( ) , 'image.jpg' ) ;
144
- expect ( createWriteStreamStub . calledWith ( finalPath ) ) . to . equal ( true ) ;
146
+ expect ( createWriteStreamStub ) . to . have . been . calledWith ( finalPath ) ;
145
147
146
148
// Ensure the image data was correctly written to the mock stream
147
149
expect ( imageData ) . to . equal ( 'image data' ) ;
@@ -165,7 +167,7 @@ describe('downloadImages.js', function () {
165
167
. to . be . rejectedWith ( 'Failed to fetch http://example.com/image.jpg. Status: 404.' ) ;
166
168
167
169
// there should be 5 retry attempts
168
- expect ( fetchStub . callCount ) . to . equal ( 5 ) ;
170
+ expect ( fetchStub ) . to . have . callCount ( 5 ) ;
169
171
170
172
// because the image fails to download, the error message should be logged
171
173
expectLogContains ( consoleErrorStub , 'Failed to download' )
@@ -183,9 +185,9 @@ describe('downloadImages.js', function () {
183
185
184
186
// test downloadImagesInMarkdown method
185
187
await downloadImagesInMarkdown ( { maxRetries : 3 , downloadLocation : 'path/to/download' } , 'path/to/markdown.md' ) ;
186
- expect ( fetchStub . calledWith ( 'http://example.com/image1.jpg' ) ) . to . equal ( true ) ;
187
- expect ( createWriteStreamStub . callCount ) . to . equal ( 3 ) ;
188
- expect ( createWriteStreamStub . called ) . to . equal ( true ) ;
188
+ expect ( fetchStub ) . to . have . been . calledWith ( 'http://example.com/image1.jpg' ) ;
189
+ expect ( createWriteStreamStub ) . to . have . been . called ;
190
+ expect ( createWriteStreamStub ) . to . have . callCount ( 3 ) ;
189
191
} ) ;
190
192
} ) ;
191
193
} ) ;
0 commit comments