-
Notifications
You must be signed in to change notification settings - Fork 103
feat: reset password feature #756
Changes from 1 commit
b89e454
97873d2
d78bd6e
da34dfc
461d079
29bc328
ba7ac6d
43927cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { getCustomerResetPasswordEndpoint } from "../../../src/endpoints"; | ||
import { apiService } from "../../../src/apiService"; | ||
import { internet, random } from "faker"; | ||
import { resetPassword, update, config } from "@shopware-pwa/shopware-6-client"; | ||
|
||
const DEFAULT_ENDPOINT = "https://shopware-2.vuestorefront.io"; | ||
const email = internet.email("John", "Doe"); | ||
const credentials = { | ||
email: email, | ||
storefrontUrl: config.endpoint ?? DEFAULT_ENDPOINT, | ||
}; | ||
|
||
jest.mock("../../../src/apiService"); | ||
const mockedAxios = apiService as jest.Mocked<typeof apiService>; | ||
|
||
describe("CustomerService - resetPassword", () => { | ||
let contextToken: string; | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
contextToken = random.uuid(); | ||
update({ contextToken }); | ||
}); | ||
afterEach(() => { | ||
expect(config.contextToken).toEqual(contextToken); | ||
}); | ||
|
||
it("rejects the promise if the email do not mach any in Sales Channel", async () => { | ||
mockedAxios.post.mockRejectedValueOnce( | ||
new Error("400 - invalid email address") | ||
); | ||
expect( | ||
resetPassword({ | ||
email: credentials.email, | ||
storefrontUrl: credentials.storefrontUrl ?? "", | ||
}) | ||
).rejects.toThrow("400 - invalid email"); | ||
expect(mockedAxios.post).toBeCalledTimes(1); | ||
expect(mockedAxios.post).toBeCalledWith( | ||
getCustomerResetPasswordEndpoint(), | ||
{ | ||
email: credentials.email, | ||
storefrontUrl: credentials.storefrontUrl, | ||
} | ||
); | ||
}); | ||
|
||
it("returns no data if successfully updated", async () => { | ||
mockedAxios.post.mockResolvedValueOnce(null); | ||
const result = await resetPassword({ | ||
email: credentials.email, | ||
storefrontUrl: credentials.storefrontUrl ?? "", | ||
}); | ||
expect(result).toBeFalsy(); | ||
expect(mockedAxios.post).toBeCalledTimes(1); | ||
expect(mockedAxios.post).toBeCalledWith( | ||
getCustomerResetPasswordEndpoint(), | ||
{ | ||
email: credentials.email, | ||
storefrontUrl: credentials.storefrontUrl ?? "", | ||
} | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,7 @@ import { | |||||
getCustomerRegisterEndpoint, | ||||||
getCustomerDetailsUpdateEndpoint, | ||||||
getCustomerUpdatePasswordEndpoint, | ||||||
getCustomerResetPasswordEndpoint, | ||||||
getCustomerDefaultBillingAddressEndpoint, | ||||||
getCustomerDefaultShippingAddressEndpoint, | ||||||
getCustomerLogoutEndpoint, | ||||||
|
@@ -14,6 +15,7 @@ import { | |||||
} from "../endpoints"; | ||||||
import { Customer } from "@shopware-pwa/commons/interfaces/models/checkout/customer/Customer"; | ||||||
import { apiService } from "../apiService"; | ||||||
import { config } from "../settings"; | ||||||
import { CustomerAddress } from "@shopware-pwa/commons/interfaces/models/checkout/customer/CustomerAddress"; | ||||||
import { CustomerRegistrationParams } from "@shopware-pwa/commons/interfaces/request/CustomerRegistrationParams"; | ||||||
import { ContextTokenResponse } from "@shopware-pwa/commons/interfaces/response/SessionContext"; | ||||||
|
@@ -238,6 +240,30 @@ export async function updatePassword( | |||||
await apiService.post(getCustomerUpdatePasswordEndpoint(), params); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @alpha | ||||||
*/ | ||||||
export interface CustomerResetPasswordParam { | ||||||
email: string; | ||||||
storefrontUrl?: string; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Reset a customer's password | ||||||
* | ||||||
* @throws ClientApiError | ||||||
* @alpha | ||||||
*/ | ||||||
export async function resetPassword( | ||||||
params: CustomerResetPasswordParam | ||||||
): Promise<void> { | ||||||
if (!params.storefrontUrl) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in case of params being null we should prevent error.
Suggested change
more about: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining const result = await resetPassword(null as any); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Just added a workaround with
I do not get this.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's what I meant with this test, turned out it has to be another if statement. What we needed to test:
please take a look at this commit: 29bc328 Why should we test for 2? Because library compiles to JS and someone might unintentionally ignore param for method. We need to be resistant to this kind of behaviour:) |
||||||
params.storefrontUrl = config.endpoint; | ||||||
} | ||||||
|
||||||
await apiService.post(getCustomerResetPasswordEndpoint(), params); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @alpha | ||||||
*/ | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is perfectly okay, more educational improvement here you can do
https://jestjs.io/docs/en/mock-function-api#mockfnmockreturnvalueoncevalue
and in case of mocking rejections like in line 513
https://jestjs.io/docs/en/mock-function-api#mockfnmockrejectedvalueoncevalue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some good hints. Thank you :)