1
0
Fork 0
mirror of https://code.forgejo.org/actions/cache.git synced 2024-11-15 03:11:17 +01:00
cache/__tests__/stateProvider.test.ts

89 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-12-12 10:53:50 +01: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 () => {
const getStateMock = jest
.spyOn(core, "getState")
.mockImplementation(name =>
jest.requireActual("@actions/core").getState(name)
);
2022-12-12 10:53:50 +01:00
const saveStateMock = jest
.spyOn(core, "saveState")
.mockImplementation((key, value) => {
return jest.requireActual("@actions/core").saveState(key, value);
});
const setOutputMock = jest
.spyOn(core, "setOutput")
.mockImplementation((key, value) => {
return jest.requireActual("@actions/core").setOutput(key, value);
});
2022-12-12 10:53:50 +01:00
const cacheMatchedKey = "node-cache";
const stateProvider: IStateProvider = new StateProvider();
stateProvider.setState("stateKey", "stateValue");
stateProvider.setState(State.CacheMatchedKey, cacheMatchedKey);
stateProvider.getState("stateKey");
stateProvider.getCacheState();
expect(getStateMock).toHaveBeenCalledTimes(2);
expect(saveStateMock).toHaveBeenCalledTimes(2);
expect(setOutputMock).toHaveBeenCalledTimes(0);
2022-12-12 10:53:50 +01:00
});
test("NullStateProvider saves outputs", async () => {
const getStateMock = jest
.spyOn(core, "getState")
.mockImplementation(name =>
jest.requireActual("@actions/core").getState(name)
);
2022-12-12 10:53:50 +01: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 10:53:50 +01:00
const cacheMatchedKey = "node-cache";
const nullStateProvider: IStateProvider = new NullStateProvider();
2022-12-12 11:01:21 +01:00
nullStateProvider.setState(State.CacheMatchedKey, "outputValue");
nullStateProvider.setState(State.CachePrimaryKey, cacheMatchedKey);
2022-12-12 10:53:50 +01:00
nullStateProvider.getState("outputKey");
nullStateProvider.getCacheState();
expect(getStateMock).toHaveBeenCalledTimes(0);
expect(setOutputMock).toHaveBeenCalledTimes(2);
expect(saveStateMock).toHaveBeenCalledTimes(0);
2022-12-12 10:53:50 +01:00
});