Skip to content

Commit

Permalink
test: adapt for latest code
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy1339 committed Feb 24, 2024
1 parent ef1f6be commit 3aa6dab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"lint": "prettier --check '{src,test,scripts}/**/*.{ts,md}' README.md *.json",
"lint:fix": "prettier --write '{src,test,scripts}/**/*.{ts,md}' README.md *.json",
"pretest": "npm run -s lint",
"test": "jest --coverage",
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2018 --moduleResolution node test/typescript-validate.ts"
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest --coverage",
"test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --strict --target es2022 --module node16 --moduleResolution node16 test/typescript-validate.ts"
},
"repository": "github:octokit/auth-app.js",
"keywords": [
Expand Down
50 changes: 33 additions & 17 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import fetchMock, { type MockMatcherFunction } from "fetch-mock";

import { request } from "@octokit/request";
import { install } from "@sinonjs/fake-timers";
import { jest } from "@jest/globals";

import { createAppAuth, createOAuthUserAuth } from "../src/index.ts";
import type { FactoryInstallation } from "../src/types.ts";

const APP_ID = 1;
const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
Expand Down Expand Up @@ -1176,10 +1178,14 @@ test("caches based on installation id", async () => {

test("supports custom cache", async () => {
const CACHE: { [key: string]: string } = {};
const get = jest.fn().mockImplementation((key) => CACHE[key]);
const set = jest.fn().mockImplementation((key, value) => {
CACHE[key] = value;
});
const get = jest
.fn<(key: string) => string>()
.mockImplementation((key) => CACHE[key]);
const set = jest
.fn<(key: string, value: string) => void>()
.mockImplementation((key, value) => {
CACHE[key] = value;
});

const requestMock = request.defaults({
headers: {
Expand Down Expand Up @@ -1243,8 +1249,8 @@ test("supports custom cache", async () => {

expect(get).toHaveBeenCalledTimes(4);
expect(set).toHaveBeenCalledTimes(3);
expect(get).toBeCalledWith("123");
expect(set).toBeCalledWith(
expect(get).toHaveBeenCalledWith("123");
expect(set).toHaveBeenCalledWith(
"123",
"secret123|1970-01-01T00:00:00.000Z|1970-01-01T01:00:00.000Z|all|metadata|",
);
Expand All @@ -1260,10 +1266,14 @@ test("supports custom cache", async () => {

test("supports custom cache with async get/set", async () => {
const CACHE: { [key: string]: string } = {};
const get = jest.fn().mockImplementation(async (key) => CACHE[key]);
const set = jest.fn().mockImplementation(async (key, value) => {
CACHE[key] = value;
});
const get = jest
.fn<(key: string) => Promise<string>>()
.mockImplementation(async (key) => CACHE[key]);
const set = jest
.fn<(key: string, value: string) => Promise<void>>()
.mockImplementation(async (key, value) => {
CACHE[key] = value;
});

const requestMock = request.defaults({
headers: {
Expand Down Expand Up @@ -1305,8 +1315,8 @@ test("supports custom cache with async get/set", async () => {

expect(get).toHaveBeenCalledTimes(2);
expect(set).toHaveBeenCalledTimes(1);
expect(get).toBeCalledWith("123");
expect(set).toBeCalledWith(
expect(get).toHaveBeenCalledWith("123");
expect(set).toHaveBeenCalledWith(
"123",
"secret123|1970-01-01T00:00:00.000Z|1970-01-01T01:00:00.000Z|all|metadata|",
);
Expand Down Expand Up @@ -2006,10 +2016,14 @@ test("oauth endpoint error", async () => {

test("auth.hook() and custom cache", async () => {
const CACHE: { [key: string]: string } = {};
const get = jest.fn().mockImplementation(async (key) => CACHE[key]);
const set = jest.fn().mockImplementation(async (key, value) => {
CACHE[key] = value;
});
const get = jest
.fn<(key: string) => Promise<string>>()
.mockImplementation(async (key) => CACHE[key]);
const set = jest
.fn<(key: string, value: string) => Promise<void>>()
.mockImplementation(async (key, value) => {
CACHE[key] = value;
});

const mock = fetchMock
.sandbox()
Expand Down Expand Up @@ -2200,7 +2214,9 @@ test("factory auth option", async () => {
extra2: "value2",
});

const factory = jest.fn().mockReturnValue({ token: "secret" });
const factory = jest
.fn<FactoryInstallation<any>>()
.mockReturnValue({ token: "secret" });

const customAuth = await appAuth({
type: "installation",
Expand Down

0 comments on commit 3aa6dab

Please sign in to comment.