Skip to content

Commit c0f3325

Browse files
committed
cypress tests
1 parent 9421556 commit c0f3325

File tree

7 files changed

+394
-6
lines changed

7 files changed

+394
-6
lines changed

packages/messenger-widget/cypress.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export default defineConfig({
77
bundler: 'webpack',
88
webpackConfig: require('./webpack.config'),
99
},
10-
specPattern: '**/*.cy.{js,jsx,ts,tsx}',
10+
specPattern: './src/**/*.cy.{js,jsx,ts,tsx}',
1111
},
1212
});

packages/messenger-widget/cypress/support/component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
// Import commands.js using ES2015 syntax:
1717
import './commands';
18+
window.Buffer = window.Buffer || require('buffer').Buffer;
1819

1920
// Alternatively you can use CommonJS syntax:
2021
// require('./commands')
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+
});

packages/messenger-widget/src/context/testHelper/getMockedDm3Configuration.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
1-
import {
2-
DM3ConfigurationContext,
3-
DM3ConfigurationContextType,
4-
} from '../DM3ConfigurationContext';
1+
import { SiweValidityStatus } from '../../utils/enum-type-utils';
2+
import { DM3Configuration, Siwe } from '../../widget';
3+
import { DM3ConfigurationContextType } from '../DM3ConfigurationContext';
54

65
//Provide a mocked DM3Configuration context
76
//Override the default values with the provided values
87
export const getMockedDm3Configuration = (
98
override?: Partial<DM3ConfigurationContextType>,
109
) => {
11-
return { ...DM3ConfigurationContext, ...override };
10+
const defaultValue = {
11+
setDm3Configuration: (configuration: DM3Configuration) => {},
12+
dm3Configuration: {
13+
defaultContact: '',
14+
defaultServiceUrl: '',
15+
ethereumProvider: '',
16+
walletConnectProjectId: '',
17+
userEnsSubdomain: '',
18+
addressEnsSubdomain: '',
19+
resolverBackendUrl: '',
20+
profileBaseUrl: '',
21+
defaultDeliveryService: '',
22+
backendUrl: '',
23+
chainId: '',
24+
showAlways: true,
25+
showContacts: true,
26+
publicVapidKey: '',
27+
nonce: '',
28+
},
29+
screenWidth: window.innerWidth,
30+
setScreenWidth: (width: number) => {},
31+
siweValidityStatus: SiweValidityStatus.TO_BE_INITIATED,
32+
setSiweValidityStatus: (status: SiweValidityStatus) => {},
33+
validateSiweCredentials: (data: Siwe) => {},
34+
};
35+
36+
return { ...defaultValue, ...override };
1237
};
1338

1439
export const DEFAULT_DM3_CONFIGURATION = {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { MessageContextType } from '../MessageContext';
2+
3+
//Provide a mocked Message context
4+
//Override the default values with the provided values
5+
export const getMockedMessageContext = (
6+
override?: Partial<MessageContextType>,
7+
) => {
8+
const defaultValues = {
9+
getMessages: (contact: string) => [],
10+
getUnreadMessageCount: (contact: string) => 0,
11+
addMessage: (contact: string, message: any) =>
12+
new Promise(() => {
13+
isSuccess: true;
14+
}),
15+
loadMoreMessages: (contact: string) =>
16+
new Promise(() => {
17+
return 0;
18+
}),
19+
contactIsLoading: (contact: string) => false,
20+
messages: {},
21+
};
22+
23+
return { ...defaultValues, ...override };
24+
};
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { ModalContextType } from '../ModalContext';
2+
import { NewContact } from './../../interfaces/utils';
3+
import {
4+
MessageActionType,
5+
ProfileScreenType,
6+
ProfileType,
7+
} from './../../utils/enum-type-utils';
8+
import {
9+
IConfigureProfileModal,
10+
IOpenEmojiPopup,
11+
PreferencesOptionType,
12+
} from './../../hooks/modals/useModal';
13+
import { PREFERENCES_ITEMS } from './../../components/Preferences/bl';
14+
15+
//Provide a mocked Modal context
16+
//Override the default values with the provided values
17+
export const getMockedModalContext = (override?: Partial<ModalContextType>) => {
18+
const defaultValues = {
19+
loaderContent: '',
20+
setLoaderContent: (content: string) => {
21+
throw new Error('Function not implemented.');
22+
},
23+
contactToHide: undefined,
24+
setContactToHide: (action: string | undefined) => {
25+
throw new Error('Function not implemented.');
26+
},
27+
addConversation: {
28+
active: false,
29+
ensName: undefined,
30+
processed: false,
31+
},
32+
setAddConversation: (contact: NewContact) => {
33+
throw new Error('Function not implemented.');
34+
},
35+
openEmojiPopup: { action: false, data: undefined },
36+
setOpenEmojiPopup: (action: IOpenEmojiPopup) => {
37+
throw new Error('Function not implemented.');
38+
},
39+
lastMessageAction: MessageActionType.NONE,
40+
setLastMessageAction: (action: MessageActionType) => {
41+
throw new Error('Function not implemented.');
42+
},
43+
showProfileConfigurationModal: false,
44+
setShowProfileConfigurationModal: (show: boolean) => {
45+
throw new Error('Function not implemented.');
46+
},
47+
showPreferencesModal: false,
48+
setShowPreferencesModal: (show: boolean) => {
49+
throw new Error('Function not implemented.');
50+
},
51+
showAboutModal: false,
52+
setShowAboutModal: (show: boolean) => {
53+
throw new Error('Function not implemented.');
54+
},
55+
showAddConversationModal: false,
56+
setShowAddConversationModal: (show: boolean) => {
57+
throw new Error('Function not implemented.');
58+
},
59+
configureProfileModal: {
60+
profileOptionSelected: ProfileType.DM3_NAME,
61+
onScreen: ProfileScreenType.NONE,
62+
},
63+
setConfigureProfileModal: (modal: IConfigureProfileModal) => {
64+
throw new Error('Function not implemented.');
65+
},
66+
resetConfigureProfileModal: () => {
67+
throw new Error('Function not implemented.');
68+
},
69+
resetModalStates: () => {
70+
throw new Error('Function not implemented.');
71+
},
72+
preferencesOptionSelected: null,
73+
setPreferencesOptionSelected: (item: PreferencesOptionType | null) => {
74+
throw new Error('Function not implemented.');
75+
},
76+
preferencesOptions: [],
77+
updatePreferenceSelected: (ticker: PREFERENCES_ITEMS | null) => {
78+
throw new Error('Function not implemented.');
79+
},
80+
disabledOptions: {
81+
notification: {
82+
email: false,
83+
push: false,
84+
},
85+
profile: {
86+
dm3: [
87+
{ key: 'dm3', value: false },
88+
{ key: 'optimism', value: false },
89+
],
90+
own: [
91+
{ key: 'ens', value: false },
92+
{ key: 'gnosis', value: false },
93+
],
94+
},
95+
settings: {
96+
messageView: false,
97+
},
98+
},
99+
isProfileDialogDisabled: () => false,
100+
};
101+
102+
return { ...defaultValues, ...override };
103+
};

0 commit comments

Comments
 (0)