// Copyright (c) 2023 Tudor Stanciu

import React from "react";
import { renderHook } from "@testing-library/react";
import { TuitioContext } from "../../src/contexts";
import { useTuitioDecodedToken } from "../../src/hooks";

const tokenMock =
  "eyJUb2tlbklkIjoiZDkzZjEwMzQtNjI4ZS00MWQ4LWI5OGUtNGM3MDhiMmRlODgzIiwiVXNlcklkIjoxLCJVc2VyTmFtZSI6InR1aXRpby51c2VyIiwiRmlyc3ROYW1lIjoiVHVpdGlvIiwiTGFzdE5hbWUiOiJVc2VyIiwiRW1haWwiOiJ0dWl0aW8udXNlckBsYWIuY29tIiwiU2VjdXJpdHlTdGFtcCI6InRlc3Qtc2VjdXJpdHktc3RhbXAiLCJMb2NrU3RhbXAiOiJ0ZXN0LWxvY2stc3RhbXAiLCJDcmVhdGVkQXQiOiIyMDIwLTAxLTI5VDE1OjIyOjQ5LjYyMzk5MzFaIiwiRXhwaXJlc0luIjoxMDYyfQ==";

describe("useTuitioDecodedToken tests", () => {
  const testState = {
    auth: { userName: "test-user", token: tokenMock, validUntil: new Date() },
    configuration: {
      tuitioUrl: null
    }
  };

  testState.auth.validUntil.setHours(testState.auth.validUntil.getHours() + 1);

  const wrapper = ({ children }: { children?: React.ReactNode }) => (
    <TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
  );

  it("should successfully decode token", () => {
    const { result } = renderHook(() => useTuitioDecodedToken(), { wrapper });
    const { decodedToken } = result.current;
    expect(decodedToken?.userId).toEqual(1);
    expect(decodedToken?.userName).toBe("tuitio.user");
    expect(decodedToken?.firstName).toBe("Tuitio");
    expect(decodedToken?.securityStamp).toBeDefined();
    expect(decodedToken?.lockStamp).toBeDefined();
    expect(decodedToken?.createdAt).toBeDefined();
  });
});
