
* Use generated hooks in apps * Remove unused files * Use proper types in apps * Use generated hooks in attributes * Use generated hooks in auth module * Use generated hooks in categories * Use generated hooks in channels * Use generated types in collections * Remove legacy types from background tasks * Use generated hooks in customers * Use generated hooks in discounts * Use generated hook in file upload * Use generated types in gift cards * Use generated types in home * Use generated hooks in navigation * Use generated hooks in orders * Use generated hooks in pages * Use generated hooks in page types * Use generated hooks in permission groups * Use generated hooks in plugins * Use generated hooks in products * Use fragment to mark product variants * Improve code a bit * Use generated hooks in page types * Use generated types in searches * Use generated hooks in shipping * Use generated hooks in site settings * Use generated hooks in staff members * Use generated hooks in taxes * Place all gql generated files in one directory * Use generated hooks in translations * Use global types from new generated module * Use generated hooks in warehouses * Use generated hooks in webhooks * Use generated fragment types * Unclutter types * Remove hoc components * Split hooks and types * Fetch introspection file * Delete obsolete schema file * Fix rebase artifacts * Fix autoreplace * Fix auth provider tests * Fix urls * Remove leftover types * Fix rebase artifacts
98 lines
2.4 KiB
TypeScript
98 lines
2.4 KiB
TypeScript
import { createSaleorClient, SaleorProvider } from "@saleor/sdk";
|
|
import setupApi from "@test/api";
|
|
import { act, renderHook } from "@testing-library/react-hooks";
|
|
import React from "react";
|
|
import { IntlProvider } from "react-intl";
|
|
import { MemoryRouter as Router } from "react-router-dom";
|
|
|
|
import { useAuthProvider } from "./hooks/useAuthProvider";
|
|
|
|
const apolloClient = setupApi();
|
|
|
|
function renderAuthProvider() {
|
|
const intl = {
|
|
formatMessage: ({ defaultMessage }) => defaultMessage
|
|
};
|
|
const notify = jest.fn();
|
|
const saleorClient = createSaleorClient({
|
|
apiUrl: process.env.API_URI || "http://localhost:8000/graphql/",
|
|
channel: ""
|
|
});
|
|
const wrapper = ({ children }) => (
|
|
<IntlProvider defaultLocale="en" locale="en">
|
|
<Router>
|
|
<SaleorProvider client={saleorClient}>{children}</SaleorProvider>
|
|
</Router>
|
|
</IntlProvider>
|
|
);
|
|
|
|
const { result } = renderHook(
|
|
() => useAuthProvider({ intl: intl as any, notify, apolloClient }),
|
|
{ wrapper }
|
|
);
|
|
|
|
return result;
|
|
}
|
|
|
|
const adminCredentials = {
|
|
email: "admin@example.com",
|
|
password: "admin",
|
|
token: null
|
|
};
|
|
|
|
const nonStaffUserCredentials = {
|
|
email: "client@example.com",
|
|
password: "password"
|
|
};
|
|
|
|
beforeEach(() => {
|
|
localStorage.clear();
|
|
sessionStorage.clear();
|
|
});
|
|
|
|
describe("User", () => {
|
|
it("will be logged in if has valid credentials", async done => {
|
|
const hook = renderAuthProvider();
|
|
|
|
await act(async () => {
|
|
const result = await hook.current.login(
|
|
adminCredentials.email,
|
|
adminCredentials.password
|
|
);
|
|
expect(result.user?.email).toBe(adminCredentials.email);
|
|
});
|
|
expect(hook.current.authenticated).toBe(true);
|
|
|
|
done();
|
|
});
|
|
|
|
it("will not be logged in if doesn't have valid credentials", async done => {
|
|
const hook = renderAuthProvider();
|
|
|
|
await act(async () => {
|
|
const result = await hook.current.login(
|
|
adminCredentials.email,
|
|
"NotAValidPassword123!"
|
|
);
|
|
expect(result.user).toBe(null);
|
|
});
|
|
expect(hook.current.authenticated).toBe(false);
|
|
|
|
done();
|
|
});
|
|
|
|
it("will not be logged in if is non-staff", async done => {
|
|
const hook = renderAuthProvider();
|
|
|
|
await act(async () => {
|
|
const result = await hook.current.login(
|
|
nonStaffUserCredentials.email,
|
|
nonStaffUserCredentials.password
|
|
);
|
|
expect(result.user).toBe(null);
|
|
});
|
|
expect(hook.current.authenticated).toBe(false);
|
|
|
|
done();
|
|
});
|
|
});
|