2022-07-12 10:55:28 +02:00
import { config , mount , VueWrapper } from "@vue/test-utils" ;
2020-12-07 14:48:48 +01:00
import ParticipationWithoutAccount from "@/components/Participation/ParticipationWithoutAccount.vue" ;
import { routes } from "@/router" ;
import {
CommentModeration ,
EventJoinOptions ,
ParticipantRole ,
} from "@/types/enums" ;
import {
createMockClient ,
MockApolloClient ,
RequestHandler ,
} from "mock-apollo-client" ;
2022-07-12 10:55:28 +02:00
import { ANONYMOUS_ACTOR_ID } from "@/graphql/config" ;
2020-12-07 14:48:48 +01:00
import { FETCH_EVENT_BASIC , JOIN_EVENT } from "@/graphql/event" ;
import { IEvent } from "@/types/event.model" ;
2022-07-12 10:55:28 +02:00
import { anonymousActorIdMock } from "../../mocks/config" ;
2020-12-07 14:48:48 +01:00
import {
fetchEventBasicMock ,
joinEventMock ,
joinEventResponseMock ,
} from "../../mocks/event" ;
2021-06-04 20:24:43 +02:00
import { defaultResolvers } from "../../common" ;
import flushPromises from "flush-promises" ;
2022-07-12 10:55:28 +02:00
import { vi , describe , expect , it , beforeEach , afterEach } from "vitest" ;
import { DefaultApolloClient } from "@vue/apollo-composable" ;
import { Router , createRouter , createWebHistory } from "vue-router" ;
import Oruga from "@oruga-ui/oruga-next" ;
import { cache } from "@/apollo/memory" ;
2020-12-07 14:48:48 +01:00
2022-07-12 10:55:28 +02:00
config . global . plugins . push ( Oruga ) ;
let router : Router ;
beforeEach ( async ( ) = > {
router = createRouter ( {
history : createWebHistory ( ) ,
routes : routes ,
} ) ;
// await router.isReady();
} ) ;
2020-12-07 14:48:48 +01:00
const eventData = {
id : "1" ,
uuid : "f37910ea-fd5a-4756-9679-00971f3f4106" ,
options : {
commentModeration : CommentModeration.ALLOW_ALL ,
} ,
joinOptions : EventJoinOptions.FREE ,
beginsOn : new Date ( "2089-12-04T09:21:25Z" ) ,
endsOn : new Date ( "2089-12-04T11:21:25Z" ) ,
participantStats : {
notApproved : 0 ,
notConfirmed : 0 ,
rejected : 0 ,
participant : 0 ,
creator : 1 ,
moderator : 0 ,
administrator : 0 ,
going : 1 ,
} ,
} ;
describe ( "ParticipationWithoutAccount" , ( ) = > {
2022-07-12 10:55:28 +02:00
let wrapper : VueWrapper ;
let mockClient : MockApolloClient | null ;
2020-12-07 14:48:48 +01:00
let requestHandlers : Record < string , RequestHandler > ;
2022-07-12 10:55:28 +02:00
afterEach ( ( ) = > {
wrapper ? . unmount ( ) ;
cache . reset ( ) ;
mockClient = null ;
} ) ;
2020-12-07 14:48:48 +01:00
const generateWrapper = (
handlers : Record < string , unknown > = { } ,
2022-07-12 10:55:28 +02:00
customProps : Record < string , unknown > = { }
2020-12-07 14:48:48 +01:00
) = > {
mockClient = createMockClient ( {
cache ,
2021-06-04 20:24:43 +02:00
resolvers : defaultResolvers ,
2020-12-07 14:48:48 +01:00
} ) ;
requestHandlers = {
2022-07-12 10:55:28 +02:00
anonymousActorIdQueryHandler : vi
. fn ( )
. mockResolvedValue ( anonymousActorIdMock ) ,
fetchEventQueryHandler : vi.fn ( ) . mockResolvedValue ( fetchEventBasicMock ) ,
joinEventMutationHandler : vi
2020-12-07 14:48:48 +01:00
. fn ( )
. mockResolvedValue ( joinEventResponseMock ) ,
. . . handlers ,
} ;
2022-07-12 10:55:28 +02:00
mockClient . setRequestHandler (
ANONYMOUS_ACTOR_ID ,
requestHandlers . anonymousActorIdQueryHandler
) ;
2020-12-07 14:48:48 +01:00
mockClient . setRequestHandler (
FETCH_EVENT_BASIC ,
requestHandlers . fetchEventQueryHandler
) ;
mockClient . setRequestHandler (
JOIN_EVENT ,
requestHandlers . joinEventMutationHandler
) ;
wrapper = mount ( ParticipationWithoutAccount , {
2022-07-12 10:55:28 +02:00
props : {
2020-12-07 14:48:48 +01:00
uuid : eventData.uuid ,
. . . customProps ,
} ,
2022-07-12 10:55:28 +02:00
global : {
provide : {
[ DefaultApolloClient ] : mockClient ,
} ,
plugins : [ router ] ,
2020-12-07 14:48:48 +01:00
} ,
} ) ;
} ;
it ( "renders the participation without account view with minimal data" , async ( ) = > {
generateWrapper ( ) ;
expect ( wrapper . exists ( ) ) . toBe ( true ) ;
2022-07-12 10:55:28 +02:00
expect ( requestHandlers . anonymousActorIdQueryHandler ) . toHaveBeenCalled ( ) ;
2020-12-07 14:48:48 +01:00
expect ( requestHandlers . fetchEventQueryHandler ) . toHaveBeenCalledWith ( {
uuid : eventData.uuid ,
} ) ;
2022-07-12 10:55:28 +02:00
await flushPromises ( ) ;
2020-12-07 14:48:48 +01:00
2022-07-12 10:55:28 +02:00
expect ( wrapper . find ( ".container" ) . isVisible ( ) ) . toBeTruthy ( ) ;
expect ( wrapper . find ( ".o-notification--info" ) . text ( ) ) . toBe (
2020-12-07 14:48:48 +01:00
"Your email will only be used to confirm that you're a real person and send you eventual updates for this event. It will NOT be transmitted to other instances or to the event organizer."
) ;
wrapper . find ( 'input[type="email"]' ) . setValue ( "some@email.tld" ) ;
wrapper . find ( "textarea" ) . setValue ( "a message long enough" ) ;
wrapper . find ( "form" ) . trigger ( "submit" ) ;
2022-07-12 10:55:28 +02:00
await flushPromises ( ) ;
2020-12-07 14:48:48 +01:00
expect ( requestHandlers . joinEventMutationHandler ) . toHaveBeenCalledWith ( {
. . . joinEventMock ,
} ) ;
2022-07-12 10:55:28 +02:00
const cachedData = mockClient ? . cache . readQuery < { event : IEvent } > ( {
2020-12-07 14:48:48 +01:00
query : FETCH_EVENT_BASIC ,
variables : {
uuid : eventData.uuid ,
} ,
} ) ;
if ( cachedData ) {
const { event } = cachedData ;
expect ( event . participantStats . going ) . toBe (
eventData . participantStats . going + 1
) ;
expect ( event . participantStats . participant ) . toBe (
eventData . participantStats . participant + 1
) ;
}
2021-06-04 20:24:43 +02:00
await flushPromises ( ) ;
2020-12-07 14:48:48 +01:00
expect ( wrapper . find ( "form" ) . exists ( ) ) . toBeFalsy ( ) ;
expect ( wrapper . find ( "h1.title" ) . text ( ) ) . toBe (
"Request for participation confirmation sent"
) ;
2022-07-12 10:55:28 +02:00
// TextEncoder ~is~ was not in js-dom?
// expect(wrapper.find(".o-notification--error").text()).toBe(
// "Unable to save your participation in this browser."
// );
2020-12-07 14:48:48 +01:00
expect ( wrapper . find ( "span.details" ) . text ( ) ) . toBe (
"Your participation will be validated once you click the confirmation link into the email."
) ;
expect ( wrapper . html ( ) ) . toMatchSnapshot ( ) ;
} ) ;
it ( "renders the warning if the event participation is restricted" , async ( ) = > {
generateWrapper ( {
2022-07-12 10:55:28 +02:00
fetchEventQueryHandler : vi.fn ( ) . mockResolvedValue ( {
2020-12-07 14:48:48 +01:00
data : {
event : {
. . . fetchEventBasicMock . data . event ,
joinOptions : EventJoinOptions.RESTRICTED ,
} ,
} ,
} ) ,
2022-07-12 10:55:28 +02:00
joinEventMutationHandler : vi.fn ( ) . mockResolvedValue ( {
2020-12-07 14:48:48 +01:00
data : {
joinEvent : {
. . . joinEventResponseMock . data . joinEvent ,
role : ParticipantRole.NOT_CONFIRMED ,
} ,
} ,
} ) ,
} ) ;
2022-07-12 10:55:28 +02:00
await flushPromises ( ) ;
2020-12-07 14:48:48 +01:00
2022-07-12 10:55:28 +02:00
// expect(wrapper.vm.$data.event.joinOptions).toBe(
// EventJoinOptions.RESTRICTED
// );
2020-12-07 14:48:48 +01:00
2022-07-12 10:55:28 +02:00
expect ( wrapper . findAll ( "section.container form > p" ) [ 1 ] . text ( ) ) . toContain (
2020-12-07 14:48:48 +01:00
"The event organizer manually approves participations. Since you've chosen to participate without an account, please explain why you want to participate to this event."
) ;
2022-07-12 10:55:28 +02:00
expect (
wrapper . findAll ( "section.container form > p" ) [ 1 ] . text ( )
) . not . toContain (
2020-12-07 14:48:48 +01:00
"If you want, you may send a message to the event organizer here."
) ;
wrapper . find ( 'input[type="email"]' ) . setValue ( "some@email.tld" ) ;
wrapper . find ( "textarea" ) . setValue ( "a message long enough" ) ;
wrapper . find ( "form" ) . trigger ( "submit" ) ;
2022-07-12 10:55:28 +02:00
await flushPromises ( ) ;
2020-12-07 14:48:48 +01:00
expect ( requestHandlers . joinEventMutationHandler ) . toHaveBeenCalledWith ( {
. . . joinEventMock ,
} ) ;
2022-07-12 10:55:28 +02:00
const cachedData = mockClient ? . cache . readQuery < { event : IEvent } > ( {
2020-12-07 14:48:48 +01:00
query : FETCH_EVENT_BASIC ,
variables : {
uuid : eventData.uuid ,
} ,
} ) ;
if ( cachedData ) {
const { event } = cachedData ;
expect ( event . participantStats . notConfirmed ) . toBe (
eventData . participantStats . notConfirmed + 1
) ;
}
2021-08-02 19:26:23 +02:00
await flushPromises ( ) ;
2020-12-07 14:48:48 +01:00
expect ( wrapper . find ( "form" ) . exists ( ) ) . toBeFalsy ( ) ;
expect ( wrapper . find ( "h1.title" ) . text ( ) ) . toBe (
"Request for participation confirmation sent"
) ;
expect ( wrapper . find ( "span.details" ) . text ( ) ) . toBe (
"Your participation will be validated once you click the confirmation link into the email, and after the organizer manually validates your participation."
) ;
expect ( wrapper . html ( ) ) . toMatchSnapshot ( ) ;
} ) ;
it ( "handles being already a participant" , async ( ) = > {
generateWrapper ( {
2022-07-12 10:55:28 +02:00
joinEventMutationHandler : vi
2020-12-07 14:48:48 +01:00
. fn ( )
. mockRejectedValue (
new Error ( "You are already a participant of this event" )
) ,
} ) ;
2022-07-12 10:55:28 +02:00
await flushPromises ( ) ;
2020-12-07 14:48:48 +01:00
wrapper . find ( 'input[type="email"]' ) . setValue ( "some@email.tld" ) ;
wrapper . find ( "textarea" ) . setValue ( "a message long enough" ) ;
wrapper . find ( "form" ) . trigger ( "submit" ) ;
2022-07-12 10:55:28 +02:00
await flushPromises ( ) ;
2020-12-07 14:48:48 +01:00
expect ( requestHandlers . joinEventMutationHandler ) . toHaveBeenCalledWith ( {
. . . joinEventMock ,
} ) ;
2022-07-12 10:55:28 +02:00
await flushPromises ( ) ;
2020-12-07 14:48:48 +01:00
expect ( wrapper . find ( "form" ) . exists ( ) ) . toBeTruthy ( ) ;
2022-07-12 10:55:28 +02:00
expect ( wrapper . find ( ".o-notification--danger" ) . text ( ) ) . toContain (
"You are already a participant of this event"
) ;
2020-12-07 14:48:48 +01:00
expect ( wrapper . html ( ) ) . toMatchSnapshot ( ) ;
} ) ;
} ) ;