@@ -6,12 +6,14 @@ import DownloadButton from "./DownloadButton";
66import FileSaver from "file-saver" ;
77import JSZip from "jszip" ;
88import JSZipUtils from "jszip-utils" ;
9+ import { postMessageToScratchIframe } from "../../utils/scratchIframe" ;
910
1011jest . mock ( "file-saver" ) ;
1112jest . mock ( "jszip" ) ;
1213jest . mock ( "jszip-utils" , ( ) => ( {
1314 getBinaryContent : jest . fn ( ) ,
1415} ) ) ;
16+ jest . mock ( "../../utils/scratchIframe" ) ;
1517
1618describe ( "Downloading project with name set" , ( ) => {
1719 let downloadButton ;
@@ -181,3 +183,83 @@ describe("Downloading project with no instructions set", () => {
181183 ) ;
182184 } ) ;
183185} ) ;
186+
187+ describe ( "When project is Scratch" , ( ) => {
188+ let downloadButton ;
189+
190+ beforeEach ( ( ) => {
191+ postMessageToScratchIframe . mockClear ( ) ;
192+ JSZip . mockClear ( ) ;
193+ FileSaver . saveAs . mockClear ( ) ;
194+ const middlewares = [ ] ;
195+ const mockStore = configureStore ( middlewares ) ;
196+ const initialState = {
197+ editor : {
198+ project : {
199+ name : "Cool Scratch" ,
200+ project_type : "code_editor_scratch" ,
201+ identifier : "cool-scratch" ,
202+ components : [ ] ,
203+ image_list : [ ] ,
204+ } ,
205+ } ,
206+ } ;
207+ const store = mockStore ( initialState ) ;
208+ render (
209+ < Provider store = { store } >
210+ < DownloadButton buttonText = "Download" Icon = { ( ) => { } } />
211+ </ Provider > ,
212+ ) ;
213+ downloadButton = screen . queryByText ( "Download" ) . parentElement ;
214+ } ) ;
215+
216+ test ( "clicking download sends scratch-gui-download message with kebab filename" , ( ) => {
217+ fireEvent . click ( downloadButton ) ;
218+ expect ( postMessageToScratchIframe ) . toHaveBeenCalledTimes ( 1 ) ;
219+ expect ( postMessageToScratchIframe ) . toHaveBeenCalledWith ( {
220+ type : "scratch-gui-download" ,
221+ filename : "cool-scratch.sb3" ,
222+ } ) ;
223+ } ) ;
224+
225+ test ( "does not use JSZip or FileSaver" , ( ) => {
226+ fireEvent . click ( downloadButton ) ;
227+ expect ( JSZip ) . not . toHaveBeenCalled ( ) ;
228+ expect ( FileSaver . saveAs ) . not . toHaveBeenCalled ( ) ;
229+ } ) ;
230+ } ) ;
231+
232+ describe ( "When project is Scratch with no name" , ( ) => {
233+ let downloadButton ;
234+
235+ beforeEach ( ( ) => {
236+ postMessageToScratchIframe . mockClear ( ) ;
237+ const middlewares = [ ] ;
238+ const mockStore = configureStore ( middlewares ) ;
239+ const initialState = {
240+ editor : {
241+ project : {
242+ project_type : "code_editor_scratch" ,
243+ identifier : "cool-scratch" ,
244+ components : [ ] ,
245+ image_list : [ ] ,
246+ } ,
247+ } ,
248+ } ;
249+ const store = mockStore ( initialState ) ;
250+ render (
251+ < Provider store = { store } >
252+ < DownloadButton buttonText = "Download" Icon = { ( ) => { } } />
253+ </ Provider > ,
254+ ) ;
255+ downloadButton = screen . queryByText ( "Download" ) . parentElement ;
256+ } ) ;
257+
258+ test ( "clicking download uses default filename project.sb3" , ( ) => {
259+ fireEvent . click ( downloadButton ) ;
260+ expect ( postMessageToScratchIframe ) . toHaveBeenCalledWith ( {
261+ type : "scratch-gui-download" ,
262+ filename : "project.sb3" ,
263+ } ) ;
264+ } ) ;
265+ } ) ;
0 commit comments