@@ -16,17 +16,24 @@ import { userEvent, waitFor, within } from '@storybook/testing-library'
1616import React from 'react'
1717import * as selectEvent from 'react-select-event'
1818import styled from 'styled-components'
19+ import { useIntl } from 'react-intl'
20+ import { useSelector } from 'react-redux'
1921import {
2022 AddressType ,
2123 FieldType ,
2224 and ,
2325 not ,
2426 field as createFieldCondition ,
25- ConditionalType
27+ ConditionalType ,
28+ FieldValue ,
29+ AddressFieldValue
2630} from '@opencrvs/commons/client'
2731import { FormFieldGenerator } from '@client/v2-events/components/forms/FormFieldGenerator'
2832import { TRPCProvider } from '@client/v2-events/trpc'
33+ import { useLocations } from '@client/v2-events/hooks/useLocations'
34+ import { getOfflineData } from '@client/offline/selectors'
2935import { withValidatorContext } from '../../../../../.storybook/decorators'
36+ import { Address } from './Address'
3037
3138const meta : Meta < typeof FormFieldGenerator > = {
3239 title : 'Inputs/Address/Interaction' ,
@@ -607,3 +614,181 @@ export const AddressFieldInteractionDomesticToInternational: StoryObj<
607614 )
608615 }
609616}
617+
618+ interface ResolvedAddress {
619+ country ?: string
620+ addressType ?: string
621+ province ?: string
622+ district ?: string
623+ streetLevelDetails ?: {
624+ state ?: string
625+ district2 ?: string
626+ town ?: string
627+ cityOrTown ?: string
628+ }
629+ }
630+
631+ export const ToCertificateVariables : StoryObj < typeof FormFieldGenerator > = {
632+ name : 'Certificate Variables' ,
633+ parameters : {
634+ layout : 'centered'
635+ } ,
636+ render : function Component ( args ) {
637+ const intl = useIntl ( )
638+ const [ formData , setFormData ] = React . useState < Record < string , FieldValue > > ( {
639+ 'storybook.address' : {
640+ country : 'FAR' ,
641+ addressType : 'DOMESTIC'
642+ }
643+ } )
644+ const [ resolvedAddress , setResolvedAddress ] =
645+ React . useState < ResolvedAddress > ( )
646+ const { getLocations } = useLocations ( )
647+ const [ locations ] = getLocations . useSuspenseQuery ( )
648+ const { config : appConfig } = useSelector ( getOfflineData )
649+
650+ const adminLevels = appConfig . ADMIN_STRUCTURE
651+ return (
652+ < >
653+ < StyledFormFieldGenerator
654+ { ...args }
655+ fields = { [
656+ {
657+ id : 'storybook.address' ,
658+ type : FieldType . ADDRESS ,
659+ label : {
660+ id : 'storybook.address.label' ,
661+ defaultMessage : 'Address' ,
662+ description : 'The title for the address input'
663+ } ,
664+ configuration : {
665+ streetAddressForm : streetAddressConfigs
666+ }
667+ }
668+ ] }
669+ id = "my-form"
670+ initialValues = { formData }
671+ onChange = { ( data ) => {
672+ setFormData ( ( prev ) => ( { ...prev , ...data } ) )
673+ const address = AddressFieldValue . safeParse (
674+ data [ 'storybook.address' ]
675+ )
676+ if ( address . success ) {
677+ const resolved = Address . toCertificateVariables ( address . data , {
678+ intl,
679+ locations,
680+ adminLevels
681+ } )
682+ setResolvedAddress ( ( prev ) => ( {
683+ ...prev ,
684+ ...( resolved satisfies ResolvedAddress )
685+ } ) )
686+ }
687+ } }
688+ />
689+ { resolvedAddress && (
690+ < div >
691+ < div data-testid = "country" >
692+ { 'Country:' } { resolvedAddress . country }
693+ </ div >
694+ < div data-testid = "addressType" >
695+ { 'Address Type:' } { resolvedAddress . addressType }
696+ </ div >
697+ { resolvedAddress . province && (
698+ < div data-testid = "province" >
699+ { 'Province:' } { resolvedAddress . province }
700+ </ div >
701+ ) }
702+ { resolvedAddress . district && (
703+ < div data-testid = "district" >
704+ { 'District:' } { resolvedAddress . district }
705+ </ div >
706+ ) }
707+ { resolvedAddress . streetLevelDetails ?. state && (
708+ < div data-testid = "state" >
709+ { 'State:' } { resolvedAddress . streetLevelDetails . state }
710+ </ div >
711+ ) }
712+ { resolvedAddress . streetLevelDetails ?. district2 && (
713+ < div data-testid = "district2" >
714+ { 'District:' } { resolvedAddress . streetLevelDetails . district2 }
715+ </ div >
716+ ) }
717+
718+ { resolvedAddress . streetLevelDetails ?. town && (
719+ < div data-testid = "town" >
720+ { resolvedAddress . streetLevelDetails . town }
721+ </ div >
722+ ) }
723+ { resolvedAddress . streetLevelDetails ?. cityOrTown && (
724+ < div data-testid = "cityOrTown" >
725+ { resolvedAddress . streetLevelDetails . cityOrTown }
726+ </ div >
727+ ) }
728+ </ div >
729+ ) }
730+ </ >
731+ )
732+ } ,
733+ play : async ( { canvasElement, step } ) => {
734+ const canvas = within ( canvasElement )
735+
736+ await step ( 'Fill up Domestic Address' , async ( ) => {
737+ const province = await canvas . findByTestId ( 'location__province' )
738+ await userEvent . click ( province )
739+ await selectEvent . select ( province , 'Central' )
740+
741+ const district = await canvas . findByTestId ( 'location__district' )
742+ await userEvent . click ( district )
743+ await selectEvent . select ( district , 'Ibombo' )
744+
745+ const town = await canvas . findByTestId ( 'text__town' )
746+ await userEvent . type ( town , 'Dhaka' )
747+
748+ const residentialArea = await canvas . findByTestId ( 'text__residentialArea' )
749+ await userEvent . type ( residentialArea , 'Mirpur' )
750+
751+ await expect ( canvas . queryByTestId ( 'country' ) ) . toHaveTextContent (
752+ 'Farajaland'
753+ )
754+ await expect ( canvas . queryByTestId ( 'addressType' ) ) . toHaveTextContent (
755+ 'DOMESTIC'
756+ )
757+ await expect ( canvas . queryByTestId ( 'province' ) ) . toHaveTextContent (
758+ 'Central'
759+ )
760+ await expect ( canvas . queryByTestId ( 'district' ) ) . toHaveTextContent ( 'Ibombo' )
761+ await expect ( canvas . queryByTestId ( 'town' ) ) . toHaveTextContent ( 'Dhaka' )
762+ } )
763+
764+ await step ( 'Fill up International Address' , async ( ) => {
765+ const country = await canvas . findByTestId ( 'location__country' )
766+ await userEvent . click ( country )
767+ await selectEvent . select ( country , 'Estonia' )
768+
769+ const province = await canvas . findByTestId ( 'text__state' )
770+ await userEvent . type ( province , 'State' )
771+
772+ const district2 = await canvas . findByTestId ( 'text__district2' )
773+ await userEvent . type ( district2 , 'District' )
774+
775+ const town = await canvas . findByTestId ( 'text__cityOrTown' )
776+ await userEvent . type ( town , 'Dhaka' )
777+
778+ const residentialArea = await canvas . findByTestId ( 'text__addressLine1' )
779+ await userEvent . type ( residentialArea , 'Mirpur' )
780+
781+ await expect ( canvas . queryByTestId ( 'country' ) ) . toHaveTextContent ( 'Estonia' )
782+ await expect ( canvas . queryByTestId ( 'addressType' ) ) . toHaveTextContent (
783+ 'INTERNATIONAL'
784+ )
785+ await expect ( canvas . queryByTestId ( 'state' ) ) . toHaveTextContent ( 'State' )
786+ await expect ( canvas . queryByTestId ( 'district2' ) ) . toHaveTextContent (
787+ 'District'
788+ )
789+ await expect ( canvas . queryByTestId ( 'cityOrTown' ) ) . toHaveTextContent (
790+ 'Dhaka'
791+ )
792+ } )
793+ }
794+ }
0 commit comments