@@ -26,7 +26,7 @@ import {
2626import type { Assignment , Competition } from '@wca/helpers' ;
2727import type { Extension } from '@wca/helpers/lib/models/extension' ;
2828import { describe , expect , it , vi } from 'vitest' ;
29- import { getUpcomingManageableCompetitions , getWcif , patchWcif } from '../lib/api' ;
29+ import { checkWcif , getUpcomingManageableCompetitions , getWcif , patchWcif } from '../lib/api' ;
3030import { sortWcifEvents } from '../lib/domain/events' ;
3131import { validateWcif } from '../lib/wcif/validation' ;
3232import type { AppState } from './initialState' ;
@@ -41,6 +41,7 @@ import {
4141vi . mock ( '../lib/api' , ( ) => ( {
4242 getUpcomingManageableCompetitions : vi . fn ( ) ,
4343 getWcif : vi . fn ( ) ,
44+ checkWcif : vi . fn ( ) ,
4445 patchWcif : vi . fn ( ) ,
4546} ) ) ;
4647
@@ -54,6 +55,7 @@ vi.mock('../lib/wcif/validation', () => ({
5455
5556const getUpcomingManageableCompetitionsMock = vi . mocked ( getUpcomingManageableCompetitions ) ;
5657const getWcifMock = vi . mocked ( getWcif ) ;
58+ const checkWcifMock = vi . mocked ( checkWcif ) ;
5759const patchWcifMock = vi . mocked ( patchWcif ) ;
5860const sortWcifEventsMock = vi . mocked ( sortWcifEvents ) ;
5961const validateWcifMock = vi . mocked ( validateWcif ) ;
@@ -311,6 +313,7 @@ describe('store actions', () => {
311313 wcif,
312314 changedKeys : new Set ( [ 'events' ] ) ,
313315 } ) as unknown as AppState ;
316+ checkWcifMock . mockResolvedValueOnce ( undefined ) ;
314317 patchWcifMock . mockResolvedValueOnce ( wcif ) ;
315318
316319 uploadCurrentWCIFChanges ( cb ) ( dispatch , getState ) ;
@@ -320,6 +323,7 @@ describe('store actions', () => {
320323 type : ActionType . UPLOADING_WCIF ,
321324 uploading : true ,
322325 } ) ;
326+ expect ( checkWcifMock ) . toHaveBeenCalledWith ( wcif ) ;
323327 expect ( patchWcifMock ) . toHaveBeenCalledWith ( 'Comp1' , {
324328 formatVersion : wcif . formatVersion ,
325329 events : wcif . events ,
@@ -343,6 +347,7 @@ describe('store actions', () => {
343347 changedKeys : new Set ( [ 'events' ] ) ,
344348 } ) as unknown as AppState ;
345349 const error = new Error ( 'Upload failed' ) ;
350+ checkWcifMock . mockResolvedValueOnce ( undefined ) ;
346351 patchWcifMock . mockRejectedValueOnce ( error ) ;
347352 const consoleError = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
348353
@@ -360,4 +365,23 @@ describe('store actions', () => {
360365 expect ( cb ) . toHaveBeenCalledWith ( error ) ;
361366 consoleError . mockRestore ( ) ;
362367 } ) ;
368+
369+ it ( 'does not patch when the WCIF schema check fails' , async ( ) => {
370+ vi . clearAllMocks ( ) ;
371+ const dispatch = vi . fn ( ) ;
372+ const cb = vi . fn ( ) ;
373+ const wcif = { ...buildWcif ( [ ] , [ ] ) , id : 'Comp1' } ;
374+ const error = new Error ( 'WCIF formatVersion is required' ) ;
375+ const getState = ( ) =>
376+ ( { wcif, changedKeys : new Set ( [ 'events' ] ) } ) as unknown as AppState ;
377+ const consoleError = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
378+ checkWcifMock . mockRejectedValueOnce ( error ) ;
379+
380+ uploadCurrentWCIFChanges ( cb ) ( dispatch , getState ) ;
381+ await flushPromises ( ) ;
382+
383+ expect ( patchWcifMock ) . not . toHaveBeenCalled ( ) ;
384+ expect ( cb ) . toHaveBeenCalledWith ( error ) ;
385+ consoleError . mockRestore ( ) ;
386+ } ) ;
363387} ) ;
0 commit comments