|
| 1 | +import { Conversation } from '@dm3-org/dm3-lib-storage'; |
| 2 | +import { AuthContext, AuthContextType } from '../../context/AuthContext'; |
| 3 | +import { ConversationContext } from '../../context/ConversationContext'; |
| 4 | +import { ModalContext } from '../../context/ModalContext'; |
| 5 | +import { |
| 6 | + StorageContext, |
| 7 | + StorageContextType, |
| 8 | +} from '../../context/StorageContext'; |
| 9 | +import { getMockedAuthContext } from '../../context/testHelper/getMockedAuthContext'; |
| 10 | +import { getMockedStorageContext } from '../../context/testHelper/getMockedStorageContext'; |
| 11 | +import { UiViewContext } from '../../context/UiViewContext'; |
| 12 | +import { DM3 } from '../../widget'; |
| 13 | +import AddConversation from './AddConversation'; |
| 14 | +import { |
| 15 | + DeliveryServiceContext, |
| 16 | + DeliveryServiceContextType, |
| 17 | +} from '../../context/DeliveryServiceContext'; |
| 18 | +import { getMockedDeliveryServiceContext } from '../../context/testHelper/getMockedDeliveryServiceContext'; |
| 19 | +import { getMockedTldContext } from '../../context/testHelper/getMockedTldContext'; |
| 20 | +import { TLDContext } from '../../context/TLDContext'; |
| 21 | +import { getMockedConversationContext } from '../../context/testHelper/getMockedConversationContext'; |
| 22 | +import { getEmptyContact } from '../../interfaces/utils'; |
| 23 | +import { getMockedDm3Configuration } from '../../context/testHelper/getMockedDm3Configuration'; |
| 24 | +import { DM3ConfigurationContext } from '../../context/DM3ConfigurationContext'; |
| 25 | +import { getMockedUiViewContext } from '../../context/testHelper/getMockedUiViewContext'; |
| 26 | +import { getMockedModalContext } from '../../context/testHelper/getMockedModalContext'; |
| 27 | +import { getMockedMainnetProviderContext } from '../../context/testHelper/getMockedMainnetProviderContext'; |
| 28 | +import { MainnetProviderContext } from '../../context/ProviderContext'; |
| 29 | +import { |
| 30 | + MessageContext, |
| 31 | + MessageContextType, |
| 32 | +} from '../../context/MessageContext'; |
| 33 | +import { getMockedMessageContext } from '../../context/testHelper/getMockedMessageContext'; |
| 34 | + |
| 35 | +// AddConversation component tests |
| 36 | +describe('<AddConversation />', () => { |
| 37 | + const dm3Config = { |
| 38 | + userEnsSubdomain: process.env.REACT_APP_USER_ENS_SUBDOMAIN as string, |
| 39 | + addressEnsSubdomain: process.env.REACT_APP_ADDR_ENS_SUBDOMAIN as string, |
| 40 | + resolverBackendUrl: process.env.REACT_APP_RESOLVER_BACKEND as string, |
| 41 | + profileBaseUrl: process.env.REACT_APP_PROFILE_BASE_URL as string, |
| 42 | + defaultDeliveryService: process.env |
| 43 | + .REACT_APP_DEFAULT_DELIVERY_SERVICE as string, |
| 44 | + backendUrl: process.env.REACT_APP_BACKEND as string, |
| 45 | + chainId: process.env.REACT_APP_CHAIN_ID as string, |
| 46 | + defaultServiceUrl: process.env.REACT_APP_DEFAULT_SERVICE as string, |
| 47 | + ethereumProvider: process.env.REACT_APP_MAINNET_PROVIDER_RPC as string, |
| 48 | + walletConnectProjectId: process.env |
| 49 | + .REACT_APP_WALLET_CONNECT_PROJECT_ID as string, |
| 50 | + publicVapidKey: process.env.REACT_APP_PUBLIC_VAPID_KEY as string, |
| 51 | + defaultContact: 'defaultcontact.eth', |
| 52 | + nonce: process.env.REACT_APP_NONCE as string, |
| 53 | + showAlways: true, |
| 54 | + showContacts: true, |
| 55 | + }; |
| 56 | + |
| 57 | + // Mount the Widget component before every test |
| 58 | + beforeEach(() => { |
| 59 | + // should mount DM3 component and render it |
| 60 | + cy.mount(<DM3 {...dm3Config} />); |
| 61 | + }); |
| 62 | + |
| 63 | + // test for render AddConversation component |
| 64 | + it('Renders <AddConversation> component', () => { |
| 65 | + // should mount AddConversation component and render it |
| 66 | + cy.mount(<AddConversation />); |
| 67 | + }); |
| 68 | + |
| 69 | + // test for change in conversation name |
| 70 | + it('Should change conversation name', () => { |
| 71 | + // should mount AddConversation component and render it |
| 72 | + cy.mount(<AddConversation />); |
| 73 | + // should change conversation name input field |
| 74 | + cy.get('.conversation-name') |
| 75 | + .clear() |
| 76 | + .type('bob.eth') |
| 77 | + .should('have.value', 'bob.eth'); |
| 78 | + }); |
| 79 | + |
| 80 | + // test for invalid conversation name |
| 81 | + it('Should display error for invalid conversation name', () => { |
| 82 | + // should mount AddConversation component and render it |
| 83 | + cy.mount(<AddConversation />); |
| 84 | + // should change conversation name input field |
| 85 | + cy.get('.conversation-name').clear().type('abc-xyz#.eth'); |
| 86 | + // should show error for invalid conversation name |
| 87 | + cy.get('.conversation-error').should( |
| 88 | + 'have.text', |
| 89 | + 'Invalid address or ENS name', |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + // test to add conversation name successfully |
| 94 | + it('Should change conversation name', () => { |
| 95 | + const CONTACT_NAME = 'user.dm3.eth'; |
| 96 | + |
| 97 | + const authContext: AuthContextType = getMockedAuthContext({ |
| 98 | + account: { |
| 99 | + ensName: 'alice.eth', |
| 100 | + profile: { |
| 101 | + deliveryServices: ['ds.eth'], |
| 102 | + publicEncryptionKey: '', |
| 103 | + publicSigningKey: '', |
| 104 | + }, |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + const storageContext: StorageContextType = getMockedStorageContext({ |
| 109 | + getConversations: function (page: number): Promise<Conversation[]> { |
| 110 | + return Promise.resolve([]); |
| 111 | + }, |
| 112 | + initialized: true, |
| 113 | + }); |
| 114 | + |
| 115 | + const deliveryServiceContext: DeliveryServiceContextType = |
| 116 | + getMockedDeliveryServiceContext({ |
| 117 | + fetchIncomingMessages: function (ensName: string) { |
| 118 | + return Promise.resolve([]); |
| 119 | + }, |
| 120 | + getDeliveryServiceProperties: function (): Promise<any[]> { |
| 121 | + return Promise.resolve([{ sizeLimit: 0 }]); |
| 122 | + }, |
| 123 | + isInitialized: true, |
| 124 | + }); |
| 125 | + |
| 126 | + const tldContext = getMockedTldContext({ |
| 127 | + resolveTLDtoAlias: async (alias: string) => { |
| 128 | + return CONTACT_NAME; |
| 129 | + }, |
| 130 | + }); |
| 131 | + |
| 132 | + const conversationContext = getMockedConversationContext({ |
| 133 | + selectedContact: getEmptyContact( |
| 134 | + 'max.eth', |
| 135 | + undefined, |
| 136 | + false, |
| 137 | + 0, |
| 138 | + [], |
| 139 | + ), |
| 140 | + }); |
| 141 | + |
| 142 | + const uiViewContext = getMockedUiViewContext(); |
| 143 | + |
| 144 | + const dm3ConfigurationContext = getMockedDm3Configuration(); |
| 145 | + |
| 146 | + const modalContext = getMockedModalContext({ |
| 147 | + showAddConversationModal: true, |
| 148 | + setLoaderContent: (loader: string) => { |
| 149 | + console.log('loader active...'); |
| 150 | + }, |
| 151 | + }); |
| 152 | + |
| 153 | + const mainnetContext = getMockedMainnetProviderContext(); |
| 154 | + |
| 155 | + const messageContext = getMockedMessageContext() as MessageContextType; |
| 156 | + |
| 157 | + // should mount AddConversation component and render it |
| 158 | + cy.mount( |
| 159 | + <> |
| 160 | + <DM3ConfigurationContext.Provider |
| 161 | + value={dm3ConfigurationContext} |
| 162 | + > |
| 163 | + <UiViewContext.Provider value={uiViewContext}> |
| 164 | + <ModalContext.Provider value={modalContext}> |
| 165 | + <MainnetProviderContext.Provider |
| 166 | + value={mainnetContext} |
| 167 | + > |
| 168 | + <TLDContext.Provider value={tldContext}> |
| 169 | + <AuthContext.Provider value={authContext}> |
| 170 | + <DeliveryServiceContext.Provider |
| 171 | + value={deliveryServiceContext} |
| 172 | + > |
| 173 | + <StorageContext.Provider |
| 174 | + value={storageContext} |
| 175 | + > |
| 176 | + <ConversationContext.Provider |
| 177 | + value={conversationContext} |
| 178 | + > |
| 179 | + <MessageContext.Provider |
| 180 | + value={messageContext} |
| 181 | + > |
| 182 | + <AddConversation /> |
| 183 | + </MessageContext.Provider> |
| 184 | + </ConversationContext.Provider> |
| 185 | + </StorageContext.Provider> |
| 186 | + </DeliveryServiceContext.Provider> |
| 187 | + </AuthContext.Provider> |
| 188 | + </TLDContext.Provider> |
| 189 | + </MainnetProviderContext.Provider> |
| 190 | + </ModalContext.Provider> |
| 191 | + </UiViewContext.Provider> |
| 192 | + </DM3ConfigurationContext.Provider> |
| 193 | + </>, |
| 194 | + ); |
| 195 | + |
| 196 | + // should change conversation name input field |
| 197 | + cy.get('.conversation-name').clear().type('bob.eth'); |
| 198 | + }); |
| 199 | +}); |
0 commit comments