@@ -199,51 +199,69 @@ describe("fetchAllCategories", () => {
199199} ) ;
200200
201201describe ( "fetchPreviousYearJobCategories" , ( ) => {
202- it ( "returns null when no submitted declaration exists for previous year" , async ( ) => {
203- const { fetchPreviousYearJobCategories } = await import (
204- "../declarationHelpers"
205- ) ;
206-
207- const mockLimit = vi . fn ( ) . mockResolvedValue ( [ ] ) ;
208- const mockWhere = vi . fn ( ) . mockReturnValue ( { limit : mockLimit } ) ;
209- const mockFrom = vi . fn ( ) . mockReturnValue ( { where : mockWhere } ) ;
210- const mockSelect = vi . fn ( ) . mockReturnValue ( { from : mockFrom } ) ;
211-
212- const tx = { select : mockSelect } as never ;
213-
214- const result = await fetchPreviousYearJobCategories ( tx , "123456789" , 2026 ) ;
202+ type JobCategoryRow = {
203+ name : string ;
204+ detail : string | null ;
205+ source : string ;
206+ categoryIndex : number ;
207+ } ;
215208
216- expect ( result ) . toBeNull ( ) ;
217- } ) ;
209+ /**
210+ * Build a mocked `tx` with two select chains:
211+ * 1. declarations probe (innerJoin jobCategories, limit 1) → id or nothing
212+ * 2. jobCategories fetch for that id → rows
213+ *
214+ * Each chain resolves at its terminal call (`.limit()` / `.where()`), so
215+ * the mock is driven by the data the query is expected to return, not by
216+ * internal call-order counters.
217+ */
218+ const makeTx = (
219+ declarationId : string | null ,
220+ jobs : JobCategoryRow [ ] = [ ] ,
221+ ) => {
222+ const declarationChain = {
223+ from : ( ) => ( {
224+ innerJoin : ( ) => ( {
225+ where : ( ) => ( {
226+ orderBy : ( ) => ( {
227+ limit : ( ) =>
228+ Promise . resolve ( declarationId ? [ { id : declarationId } ] : [ ] ) ,
229+ } ) ,
230+ } ) ,
231+ } ) ,
232+ } ) ,
233+ } ;
234+
235+ const jobCategoriesChain = {
236+ from : ( ) => ( {
237+ where : ( ) => Promise . resolve ( jobs ) ,
238+ } ) ,
239+ } ;
240+
241+ const queue = [ declarationChain , jobCategoriesChain ] ;
242+ return { select : ( ) => queue . shift ( ) } as never ;
243+ } ;
218244
219- it ( "returns null when declaration exists but has no job categories " , async ( ) => {
245+ it ( "returns null when no previous declaration contains indicator 7 " , async ( ) => {
220246 const { fetchPreviousYearJobCategories } = await import (
221247 "../declarationHelpers"
222248 ) ;
223249
224- let selectCallCount = 0 ;
225- const mockLimit = vi . fn ( ) . mockResolvedValue ( [ { id : "decl-prev" } ] ) ;
226- const mockWhere = vi . fn ( ) . mockImplementation ( ( ) => {
227- selectCallCount ++ ;
228- if ( selectCallCount === 1 ) return { limit : mockLimit } ;
229- return Promise . resolve ( [ ] ) ;
230- } ) ;
231- const mockFrom = vi . fn ( ) . mockReturnValue ( { where : mockWhere } ) ;
232- const mockSelect = vi . fn ( ) . mockReturnValue ( { from : mockFrom } ) ;
233-
234- const tx = { select : mockSelect } as never ;
235-
236- const result = await fetchPreviousYearJobCategories ( tx , "123456789" , 2026 ) ;
250+ const result = await fetchPreviousYearJobCategories (
251+ makeTx ( null ) ,
252+ "123456789" ,
253+ 2026 ,
254+ ) ;
237255
238256 expect ( result ) . toBeNull ( ) ;
239257 } ) ;
240258
241- it ( "returns categories sorted by index with source from previous year " , async ( ) => {
259+ it ( "returns categories from the most recent qualifying declaration " , async ( ) => {
242260 const { fetchPreviousYearJobCategories } = await import (
243261 "../declarationHelpers"
244262 ) ;
245263
246- const mockJobs = [
264+ const tx = makeTx ( "decl-2024" , [
247265 {
248266 name : "Employés" ,
249267 detail : "Support" ,
@@ -256,19 +274,7 @@ describe("fetchPreviousYearJobCategories", () => {
256274 source : "convention-collective" ,
257275 categoryIndex : 0 ,
258276 } ,
259- ] ;
260-
261- let selectCallCount = 0 ;
262- const mockLimit = vi . fn ( ) . mockResolvedValue ( [ { id : "decl-prev" } ] ) ;
263- const mockWhere = vi . fn ( ) . mockImplementation ( ( ) => {
264- selectCallCount ++ ;
265- if ( selectCallCount === 1 ) return { limit : mockLimit } ;
266- return Promise . resolve ( mockJobs ) ;
267- } ) ;
268- const mockFrom = vi . fn ( ) . mockReturnValue ( { where : mockWhere } ) ;
269- const mockSelect = vi . fn ( ) . mockReturnValue ( { from : mockFrom } ) ;
270-
271- const tx = { select : mockSelect } as never ;
277+ ] ) ;
272278
273279 const result = await fetchPreviousYearJobCategories ( tx , "123456789" , 2026 ) ;
274280
@@ -286,26 +292,14 @@ describe("fetchPreviousYearJobCategories", () => {
286292 "../declarationHelpers"
287293 ) ;
288294
289- const mockJobs = [
295+ const tx = makeTx ( "decl-2024" , [
290296 {
291297 name : "Cadres" ,
292298 detail : null ,
293299 source : "autre" ,
294300 categoryIndex : 0 ,
295301 } ,
296- ] ;
297-
298- let selectCallCount = 0 ;
299- const mockLimit = vi . fn ( ) . mockResolvedValue ( [ { id : "decl-prev" } ] ) ;
300- const mockWhere = vi . fn ( ) . mockImplementation ( ( ) => {
301- selectCallCount ++ ;
302- if ( selectCallCount === 1 ) return { limit : mockLimit } ;
303- return Promise . resolve ( mockJobs ) ;
304- } ) ;
305- const mockFrom = vi . fn ( ) . mockReturnValue ( { where : mockWhere } ) ;
306- const mockSelect = vi . fn ( ) . mockReturnValue ( { from : mockFrom } ) ;
307-
308- const tx = { select : mockSelect } as never ;
302+ ] ) ;
309303
310304 const result = await fetchPreviousYearJobCategories ( tx , "123456789" , 2026 ) ;
311305
0 commit comments