From 4f42dc56c1d425d4207a7d17d6477f7f8197ab30 Mon Sep 17 00:00:00 2001 From: Sankalp Kotewar <98868223+kotewar@users.noreply.github.com> Date: Mon, 12 Dec 2022 09:53:50 +0000 Subject: [PATCH] Added stateprovider test cases --- __tests__/stateProvider.test.ts | 71 +++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 __tests__/stateProvider.test.ts diff --git a/__tests__/stateProvider.test.ts b/__tests__/stateProvider.test.ts new file mode 100644 index 0000000..4c588ac --- /dev/null +++ b/__tests__/stateProvider.test.ts @@ -0,0 +1,71 @@ +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) + ); + const saveStateMock = jest + .spyOn(core, "saveState") + .mockImplementation((key, value) => { + return jest.requireActual("@actions/core").saveState(key, value); + }); + + 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); +}); + +test("NullStateProvider saves outputs", async () => { + const getStateMock = jest + .spyOn(core, "getState") + .mockImplementation(name => + jest.requireActual("@actions/core").getState(name) + ); + const setOutputMock = jest + .spyOn(core, "setOutput") + .mockImplementation((key, value) => { + return jest.requireActual("@actions/core").setOutput(key, value); + }); + const cacheMatchedKey = "node-cache"; + const nullStateProvider: IStateProvider = new NullStateProvider(); + nullStateProvider.setState("outputKey", "outputValue"); + nullStateProvider.setState(State.CacheMatchedKey, cacheMatchedKey); + nullStateProvider.getState("outputKey"); + nullStateProvider.getCacheState(); + + expect(getStateMock).toHaveBeenCalledTimes(0); + expect(setOutputMock).toHaveBeenCalledTimes(2); +});