@@ -9,6 +9,7 @@ import { expect, mergeTests } from '@playwright/test'
99import {
1010 getQuestionIdsInSections ,
1111 isEmptySection ,
12+ reorderSections ,
1213} from '../../src/utils/Sections.ts'
1314import { test as randomUserTest } from '../support/fixtures/random-user.ts'
1415import { test as submitTest } from '../support/fixtures/submit.ts'
@@ -639,6 +640,170 @@ test.describe('Sections', () => {
639640 expect ( questions ) . toEqual ( [ ] )
640641 } )
641642
643+ test ( 'Sections are reordered with their questions' , ( ) => {
644+ const items = [
645+ { id : 1 , type : 'short' } ,
646+ { id : 2 , type : 'section' } ,
647+ { id : 3 , type : 'short' } ,
648+ { id : 4 , type : 'section' } ,
649+ { id : 5 , type : 'short' } ,
650+ { id : 6 , type : 'short' } ,
651+ { id : 7 , type : 'section' } ,
652+ ]
653+ const ids = ( questions : typeof items ) => questions . map ( ( { id } ) => id )
654+ expect ( ids ( reorderSections ( items , [ 4 , 7 , 2 ] ) ) ) . toEqual ( [ 1 , 4 , 5 , 6 , 7 , 2 , 3 ] )
655+ expect ( ids ( reorderSections ( items , [ 7 , 2 ] ) ) ) . toEqual ( [ 1 , 7 , 2 , 3 , 4 , 5 , 6 ] )
656+ expect ( ids ( reorderSections ( items . slice ( 0 , 1 ) , [ 2 ] ) ) ) . toEqual ( [ 1 ] )
657+ } )
658+
659+ test ( 'The reorder sections dialog moves sections with their questions on save, by button or by dragging' , async ( {
660+ page,
661+ } ) => {
662+ const form = await api ( page . request , 'POST' , '/forms' )
663+ const ids : number [ ] = [ ]
664+ for ( const [ type , text ] of [
665+ [ 'section' , 'Part one' ] ,
666+ [ 'short' , 'First' ] ,
667+ [ 'section' , 'Part two' ] ,
668+ [ 'short' , 'Second' ] ,
669+ [ 'short' , 'Third' ] ,
670+ ] ) {
671+ ids . push (
672+ (
673+ await api ( page . request , 'POST' , `/forms/${ form . id } /questions` , {
674+ type,
675+ text,
676+ } )
677+ ) . id ,
678+ )
679+ }
680+ await page . goto ( `apps/forms/${ form . hash } /edit` )
681+
682+ const firstSection = page . getByRole ( 'listitem' , { name : 'Section number 1' } )
683+ await expect (
684+ firstSection . getByRole ( 'button' , { name : / ^ M o v e / } ) ,
685+ ) . toHaveCount ( 0 )
686+ await firstSection
687+ . getByRole ( 'button' , { name : 'Actions' , exact : true } )
688+ . click ( )
689+ await page . getByRole ( 'menuitem' , { name : 'Move section' } ) . click ( )
690+
691+ const dialog = page . getByRole ( 'dialog' , { name : 'Reorder sections' } )
692+ const items = dialog . getByRole ( 'listitem' )
693+ await expect ( items ) . toHaveText ( [
694+ / P a r t o n e \s * S e c t i o n 1 o f 2 / ,
695+ / P a r t t w o \s * S e c t i o n 2 o f 2 / ,
696+ ] )
697+ const save = dialog . getByRole ( 'button' , { name : 'Save' } )
698+ await expect ( save ) . toBeDisabled ( )
699+ await expect (
700+ dialog . getByRole ( 'button' , { name : 'Move section Part one up' } ) ,
701+ ) . toBeDisabled ( )
702+
703+ const down = dialog . getByRole ( 'button' , {
704+ name : 'Move section Part one down' ,
705+ } )
706+ await down . click ( )
707+ await expect ( items ) . toHaveText ( [ / P a r t t w o / , / P a r t o n e / ] )
708+ await expect (
709+ dialog . getByRole ( 'button' , { name : 'Move section Part one up' } ) ,
710+ ) . toBeFocused ( )
711+ await dialog . getByRole ( 'button' , { name : 'Cancel' } ) . click ( )
712+ await expect ( dialog ) . toHaveCount ( 0 )
713+ await expect (
714+ firstSection . getByRole ( 'textbox' , { name : 'Title of section number 1' } ) ,
715+ ) . toHaveValue ( 'Part one' )
716+
717+ await firstSection
718+ . getByRole ( 'button' , { name : 'Actions' , exact : true } )
719+ . click ( )
720+ await page . getByRole ( 'menuitem' , { name : 'Move section' } ) . click ( )
721+ await expect ( items ) . toHaveText ( [ / P a r t o n e / , / P a r t t w o / ] )
722+ await items
723+ . nth ( 1 )
724+ . locator ( '.reorder-sections__handle' )
725+ . dragTo ( items . nth ( 0 ) , { targetPosition : { x : 20 , y : 5 } } )
726+ await expect ( items ) . toHaveText ( [ / P a r t t w o / , / P a r t o n e / ] )
727+ const order = page . waitForResponse (
728+ ( response ) =>
729+ response . request ( ) . method ( ) === 'PATCH'
730+ && response . url ( ) . endsWith ( `/forms/${ form . id } /questions` ) ,
731+ )
732+ await save . click ( )
733+ await order
734+
735+ const { questions } = await api ( page . request , 'GET' , `/forms/${ form . id } ` )
736+ expect ( questions . map ( ( { id } : { id : number } ) => id ) ) . toEqual ( [
737+ ids [ 2 ] ,
738+ ids [ 3 ] ,
739+ ids [ 4 ] ,
740+ ids [ 0 ] ,
741+ ids [ 1 ] ,
742+ ] )
743+ await expect (
744+ page . getByRole ( 'textbox' , { name : 'Title of section number 1' } ) ,
745+ ) . toHaveValue ( 'Part two' )
746+ } )
747+
748+ test ( 'Reordering sections names the moved section when a jump would go back' , async ( {
749+ page,
750+ } ) => {
751+ const form = await api ( page . request , 'POST' , '/forms' )
752+ const ids : Record < string , number > = { }
753+ for ( const [ type , text ] of [
754+ [ 'section' , 'Part one' ] ,
755+ [ 'multiple_unique' , 'Skip ahead?' ] ,
756+ [ 'section' , 'Part two' ] ,
757+ [ 'short' , 'Second' ] ,
758+ ] ) {
759+ ids [ text ] = (
760+ await api ( page . request , 'POST' , `/forms/${ form . id } /questions` , {
761+ type,
762+ text,
763+ } )
764+ ) . id
765+ }
766+ const options = await api (
767+ page . request ,
768+ 'POST' ,
769+ `/forms/${ form . id } /questions/${ ids [ 'Skip ahead?' ] } /options` ,
770+ { optionTexts : [ 'Yes' , 'No' ] } ,
771+ )
772+ await api (
773+ page . request ,
774+ 'PATCH' ,
775+ `/forms/${ form . id } /questions/${ ids [ 'Skip ahead?' ] } ` ,
776+ {
777+ keyValuePairs : {
778+ extraSettings : {
779+ goTo : [ { optionId : options [ 0 ] . id , target : ids [ 'Part two' ] } ] ,
780+ } ,
781+ } ,
782+ } ,
783+ )
784+ await page . goto ( `apps/forms/${ form . hash } /edit` )
785+
786+ await page
787+ . getByRole ( 'listitem' , { name : 'Section number 1' } )
788+ . getByRole ( 'button' , { name : 'Actions' , exact : true } )
789+ . click ( )
790+ await page . getByRole ( 'menuitem' , { name : 'Move section' } ) . click ( )
791+ const reorder = page . getByRole ( 'dialog' , { name : 'Reorder sections' } )
792+ await reorder
793+ . getByRole ( 'button' , { name : 'Move section Part two up' } )
794+ . click ( )
795+ await reorder . getByRole ( 'button' , { name : 'Save' } ) . click ( )
796+
797+ const dialog = page . getByRole ( 'dialog' , { name : 'Jumps cannot go back' } )
798+ await expect ( dialog ) . toContainText (
799+ 'Move the section "Part two" back, or remove these jumps?' ,
800+ )
801+ await dialog . getByRole ( 'button' , { name : 'Move back' } ) . click ( )
802+ await expect (
803+ page . getByRole ( 'textbox' , { name : 'Title of section number 1' } ) ,
804+ ) . toHaveValue ( 'Part one' )
805+ } )
806+
642807 test ( 'Each page checks its required questions, an empty section is an info page' , async ( {
643808 page,
644809 submitView,
0 commit comments