cache/__tests__/stateProvider.test.ts

90 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-12-12 09:53:50 +00:00
import * as core from "@actions/core";
import { Events, RefKey, State } from "../src/constants";
import {
IStateProvider,
NullStateProvider,
StateProvider
} from "../src/stateProvider";
jest.mock("@actions/core");
beforeAll(() => {
jest.spyOn(core, "getInput").mockImplementation((name, options) => {
return jest.requireActual("@actions/core").getInput(name, options);
});
jest.spyOn(core, "setOutput").mockImplementation((key, value) => {
return jest.requireActual("@actions/core").setOutput(key, value);
});
});
afterEach(() => {
delete process.env[Events.Key];
delete process.env[RefKey];
});
test("StateProvider saves states", async () => {
2022-12-19 04:23:16 +00:00
const states = new Map<string, string>();
2022-12-12 09:53:50 +00:00
const getStateMock = jest
.spyOn(core, "getState")
.mockImplementation(key => states.get(key) || "");
2022-12-12 09:53:50 +00:00
const saveStateMock = jest
.spyOn(core, "saveState")
.mockImplementation((key, value) => {
states.set(key, value);
2022-12-12 09:53:50 +00:00
});
const setOutputMock = jest
.spyOn(core, "setOutput")
.mockImplementation((key, value) => {
return jest.requireActual("@actions/core").setOutput(key, value);
});
2022-12-12 09:53:50 +00:00
const cacheMatchedKey = "node-cache";
const stateProvider: IStateProvider = new StateProvider();
stateProvider.setState("stateKey", "stateValue");
stateProvider.setState(State.CacheMatchedKey, cacheMatchedKey);
2022-12-19 04:23:16 +00:00
const stateValue = stateProvider.getState("stateKey");
const cacheStateValue = stateProvider.getCacheState();
2022-12-12 09:53:50 +00:00
expect(stateValue).toBe("stateValue");
expect(cacheStateValue).toBe(cacheMatchedKey);
2022-12-12 09:53:50 +00:00
expect(getStateMock).toHaveBeenCalledTimes(2);
expect(saveStateMock).toHaveBeenCalledTimes(2);
expect(setOutputMock).toHaveBeenCalledTimes(0);
2022-12-12 09:53:50 +00:00
});
test("NullStateProvider saves outputs", async () => {
const getStateMock = jest
.spyOn(core, "getState")
.mockImplementation(name =>
jest.requireActual("@actions/core").getState(name)
);
2022-12-12 09:53:50 +00:00
const setOutputMock = jest
.spyOn(core, "setOutput")
.mockImplementation((key, value) => {
return jest.requireActual("@actions/core").setOutput(key, value);
});
const saveStateMock = jest
.spyOn(core, "saveState")
.mockImplementation((key, value) => {
return jest.requireActual("@actions/core").saveState(key, value);
});
2022-12-12 09:53:50 +00:00
const cacheMatchedKey = "node-cache";
const nullStateProvider: IStateProvider = new NullStateProvider();
2022-12-12 10:01:21 +00:00
nullStateProvider.setState(State.CacheMatchedKey, "outputValue");
nullStateProvider.setState(State.CachePrimaryKey, cacheMatchedKey);
2022-12-12 09:53:50 +00:00
nullStateProvider.getState("outputKey");
nullStateProvider.getCacheState();
expect(getStateMock).toHaveBeenCalledTimes(0);
expect(setOutputMock).toHaveBeenCalledTimes(2);
expect(saveStateMock).toHaveBeenCalledTimes(0);
2022-12-12 09:53:50 +00:00
});