Skip to content

Commit

Permalink
feat: added mocks,stubs for auth class
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Dec 12, 2023
1 parent 09d1ac0 commit 38a604f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .meshrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ customFetch: ./custom-fetch.ts

sdk:
generateOperations:
selectionSetDepth: 2
selectionSetDepth: 2
23 changes: 23 additions & 0 deletions __mocks__/auth.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Auth } from '../src/auth/auth';
import { authStub } from '../test/stubs/auth.stub';
import { pdaStub } from '../test/stubs/pda.stub';

export const AuthMockService = (auth: Auth) => ({
checkUsernameAvailabilityMock: jest
.spyOn(auth.sdk, 'checkUsernameAvailability_query')
.mockResolvedValue({
checkUsernameAvailability: true,
}),
addEmailMock: jest.spyOn(auth.sdk, 'addEmail_mutation').mockResolvedValue({
addEmail: authStub(),
}),
addWalletMock: jest
.spyOn(auth.sdk, 'addWallet_mutation')
.mockResolvedValue({ addWallet: authStub() }),
getPDAMock: jest.spyOn(auth.sdk, 'PDA_query').mockResolvedValue({
PDA: pdaStub(),
}),
createWalletNonceMock: jest
.spyOn(auth.sdk, 'createWalletNonce_mutation')
.mockResolvedValue({ createWalletNonce: authStub() }),
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"eslint-plugin-promise": "^6.1.1",
"jest": "^29.7.0",
"prettier": "^3.1.1",
"ts-jest": "^29.1.1",
"typescript": "^5.3.3"
}
}
47 changes: 28 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { AddWalletConfirmationInput, Sdk } from '../../.mesh';
import { AuthType, Chain } from '../types';
import { errorHandler } from '../utils/errorHandler';
import {
isEmailValid,
isStringValid,
isUUIDValid,
isWalletAddressvalid,
} from '../utils/validators';

export class Auth {
private sdk: Sdk;
public sdk: Sdk;

constructor(sdk: Sdk) {
this.sdk = sdk;
Expand Down Expand Up @@ -33,6 +39,7 @@ export class Auth {
*/
async addEmail(email: string) {
try {
isEmailValid(email);
return (await this.sdk.addEmail_mutation({ input: { email } })).addEmail;
} catch (error) {
throw new Error(errorHandler(error));
Expand All @@ -48,6 +55,7 @@ export class Auth {
*/
async addEmailConfirmation(email: string, code: number) {
try {
isEmailValid(email);
return (
await this.sdk.addEmailConfirmation_mutation({
input: { code, email },
Expand All @@ -66,6 +74,7 @@ export class Auth {
*/
async addWallet(wallet: string, chain?: Chain) {
try {
isWalletAddressvalid(wallet);
return (await this.sdk.addWallet_mutation({ input: { wallet, chain } }))
.addWallet;
} catch (error) {
Expand Down Expand Up @@ -99,6 +108,7 @@ export class Auth {
*/
async createWalletNonce(wallet: string, chain?: Chain) {
try {
isWalletAddressvalid(wallet);
return (
await this.sdk.createWalletNonce_mutation({ input: { wallet, chain } })
).createWalletNonce;
Expand All @@ -114,6 +124,7 @@ export class Auth {
*/
async deleteAccount(id: string) {
try {
isUUIDValid(id);
return (await this.sdk.deleteAccount_mutation({ id })).deleteAccount;
} catch (error) {
throw new Error(errorHandler(error));
Expand All @@ -128,6 +139,7 @@ export class Auth {
*/
async loginEmail(email: string, code: number) {
try {
isEmailValid(email);
return (await this.sdk.loginEmail_mutation({ input: { email, code } }))
.loginEmail;
} catch (error) {
Expand All @@ -143,6 +155,7 @@ export class Auth {
*/
async loginWallet(wallet: string, signature: string) {
try {
isWalletAddressvalid(wallet);
return (
await this.sdk.loginWallet_mutation({
input: { wallet, signature },
Expand All @@ -160,6 +173,7 @@ export class Auth {
*/
async refreshToken(existingRefreshToken: string) {
try {
isStringValid(existingRefreshToken);
return (
await this.sdk.refreshToken_mutation({
input: { refresh_token: existingRefreshToken },
Expand All @@ -179,6 +193,7 @@ export class Auth {
*/
async unregisterAuthMethod(data: string, type: AuthType) {
try {
isStringValid(data);
return await this.sdk.unregisterAuthMethod_mutation({
input: { data, type },
});
Expand Down
Loading

0 comments on commit 38a604f

Please sign in to comment.