test(fe): add jest mocked clients
This commit is contained in:
parent
81d375eafe
commit
40c1d81d58
5 changed files with 147 additions and 35 deletions
|
@ -6,8 +6,10 @@ import {
|
|||
ListSharingsResp,
|
||||
ListSharingIDsResp,
|
||||
GetSharingDirResp,
|
||||
IFilesClient,
|
||||
} from "./";
|
||||
|
||||
import { makePromise } from "../test/helpers";
|
||||
export interface FilesClientResps {
|
||||
createMockRespID?: number;
|
||||
createMockResp?: Response;
|
||||
|
@ -34,8 +36,8 @@ export interface FilesClientResps {
|
|||
}
|
||||
|
||||
const sharingIDs = new Map<string, string>();
|
||||
sharingIDs.set("/admin/f1", "e123456");
|
||||
sharingIDs.set("/admin/f1", "f123456");
|
||||
sharingIDs.set("/defaultmock/f1", "e123456");
|
||||
sharingIDs.set("/defaultmock/f2", "f123456");
|
||||
|
||||
export const resps = {
|
||||
createMockResp: { status: 200, statusText: "", data: {} },
|
||||
|
@ -247,7 +249,7 @@ export class MockFilesClient {
|
|||
|
||||
getSharingDir = (): Promise<Response<GetSharingDirResp>> => {
|
||||
return this.wrapPromise(this.resps.getSharingDirMockResp);
|
||||
}
|
||||
};
|
||||
|
||||
isSharing = (dirPath: string): Promise<Response> => {
|
||||
return this.wrapPromise(this.resps.isSharingMockResp);
|
||||
|
@ -261,3 +263,57 @@ export class MockFilesClient {
|
|||
return this.wrapPromise(this.resps.downloadMockResp);
|
||||
};
|
||||
}
|
||||
|
||||
// JestFilesClient supports jest function mockings
|
||||
export class JestFilesClient {
|
||||
private url: string;
|
||||
constructor(url: string) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
create = jest.fn().mockReturnValueOnce(makePromise(resps.createMockResp));
|
||||
delete = jest.fn().mockReturnValueOnce(makePromise(resps.deleteMockResp));
|
||||
metadata = jest.fn().mockReturnValueOnce(makePromise(resps.metadataMockResp));
|
||||
mkdir = jest.fn().mockReturnValueOnce(makePromise(resps.mkdirMockResp));
|
||||
move = jest.fn().mockReturnValueOnce(makePromise(resps.moveMockResp));
|
||||
uploadChunk = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.uploadChunkMockResp));
|
||||
uploadStatus = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.uploadStatusMockResp));
|
||||
list = jest.fn().mockReturnValueOnce(makePromise(resps.listMockResp));
|
||||
listHome = jest.fn().mockReturnValueOnce(makePromise(resps.listHomeMockResp));
|
||||
listUploadings = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.listUploadingsMockResp));
|
||||
deleteUploading = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.deleteUploadingMockResp));
|
||||
addSharing = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.addSharingMockResp));
|
||||
deleteSharing = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.deleteSharingMockResp));
|
||||
isSharing = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.isSharingMockResp));
|
||||
listSharings = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.listSharingsMockResp));
|
||||
listSharingIDs = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.listSharingIDsMockResp));
|
||||
getSharingDir = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.getSharingDirMockResp));
|
||||
generateHash = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.generateHashMockResp));
|
||||
download = jest.fn().mockReturnValueOnce(makePromise(resps.downloadMockResp));
|
||||
}
|
||||
|
||||
export const NewMockFilesClient = (url: string): IFilesClient => {
|
||||
return new JestFilesClient(url);
|
||||
};
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
// TODO: replace this with jest mocks
|
||||
import { Response } from ".";
|
||||
|
||||
import { ClientConfigMsg } from "./";
|
||||
import { Response, ISettingsClient, ClientConfigMsg } from "./";
|
||||
import { makePromise } from "../test/helpers";
|
||||
|
||||
export interface SettingsClientResps {
|
||||
healthMockResp?: Response;
|
||||
setClientCfgMockResp?: Response;
|
||||
getClientCfgMockResp?: Response<ClientConfigMsg>;
|
||||
reportErrorsResp?: Response;
|
||||
reportErrorsMockResp?: Response;
|
||||
}
|
||||
|
||||
export const resps = {
|
||||
|
@ -29,7 +27,7 @@ export const resps = {
|
|||
},
|
||||
},
|
||||
},
|
||||
reportErrorsResp: {
|
||||
reportErrorsMockResp: {
|
||||
status: 200,
|
||||
statusText: "",
|
||||
data: {},
|
||||
|
@ -67,6 +65,28 @@ export class MockSettingsClient {
|
|||
};
|
||||
|
||||
reportErrors = (): Promise<Response> => {
|
||||
return this.wrapPromise(this.resps.reportErrorsResp);
|
||||
return this.wrapPromise(this.resps.reportErrorsMockResp);
|
||||
};
|
||||
}
|
||||
|
||||
export class JestSettingsClient {
|
||||
url: string = "";
|
||||
constructor(url: string) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
health = jest.fn().mockReturnValueOnce(makePromise(resps.healthMockResp));
|
||||
getClientCfg = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.getClientCfgMockResp));
|
||||
setClientCfg = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.setClientCfgMockResp));
|
||||
reportErrors = jest
|
||||
.fn()
|
||||
.mockReturnValueOnce(makePromise(resps.reportErrorsMockResp));
|
||||
}
|
||||
|
||||
export const NewMockSettingsClient = (url: string): ISettingsClient => {
|
||||
return new JestSettingsClient(url);
|
||||
};
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
// TODO: replace this with jest mocks
|
||||
import { Response, Quota, Preferences } from "./";
|
||||
import { makePromise } from "../test/helpers";
|
||||
import { IUsersClient, Response, Quota, Preferences } from "./";
|
||||
|
||||
export interface UsersClientResps {
|
||||
loginMockResp: Response;
|
||||
logoutMockResp: Response;
|
||||
isAuthedMockResp: Response;
|
||||
setPwdMockResp: Response;
|
||||
setUserMockResp: Response;
|
||||
forceSetPwdMockResp: Response;
|
||||
addUserMockResp: Response;
|
||||
delUserMockResp: Response;
|
||||
listUsersMockResp: Response;
|
||||
addRoleMockResp: Response;
|
||||
delRoleMockResp: Response;
|
||||
listRolesMockResp: Response;
|
||||
selfMockResp: Response;
|
||||
getCaptchaIDMockResp: Response;
|
||||
setPreferencesMockResp: Response;
|
||||
loginMockResp?: Response;
|
||||
logoutMockResp?: Response;
|
||||
isAuthedMockResp?: Response;
|
||||
setPwdMockResp?: Response;
|
||||
setUserMockResp?: Response;
|
||||
forceSetPwdMockResp?: Response;
|
||||
addUserMockResp?: Response;
|
||||
delUserMockResp?: Response;
|
||||
listUsersMockResp?: Response;
|
||||
addRoleMockResp?: Response;
|
||||
delRoleMockResp?: Response;
|
||||
listRolesMockResp?: Response;
|
||||
selfMockResp?: Response;
|
||||
getCaptchaIDMockResp?: Response;
|
||||
setPreferencesMockResp?: Response;
|
||||
}
|
||||
|
||||
export const resps = {
|
||||
|
@ -187,3 +187,36 @@ export class MockUsersClient {
|
|||
return this.wrapPromise(this.resps.setPreferencesMockResp);
|
||||
};
|
||||
}
|
||||
|
||||
export class JestUsersClient {
|
||||
url: string = "";
|
||||
constructor(url: string) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
login = jest.fn().mockReturnValue(makePromise(resps.loginMockResp));
|
||||
logout = jest.fn().mockReturnValue(makePromise(resps.logoutMockResp));
|
||||
isAuthed = jest.fn().mockReturnValue(makePromise(resps.isAuthedMockResp));
|
||||
self = jest.fn().mockReturnValue(makePromise(resps.selfMockResp));
|
||||
setPwd = jest.fn().mockReturnValue(makePromise(resps.setPwdMockResp));
|
||||
setUser = jest.fn().mockReturnValue(makePromise(resps.setUserMockResp));
|
||||
forceSetPwd = jest
|
||||
.fn()
|
||||
.mockReturnValue(makePromise(resps.forceSetPwdMockResp));
|
||||
addUser = jest.fn().mockReturnValue(makePromise(resps.addUserMockResp));
|
||||
delUser = jest.fn().mockReturnValue(makePromise(resps.delUserMockResp));
|
||||
listUsers = jest.fn().mockReturnValue(makePromise(resps.listUsersMockResp));
|
||||
addRole = jest.fn().mockReturnValue(makePromise(resps.addRoleMockResp));
|
||||
delRole = jest.fn().mockReturnValue(makePromise(resps.delRoleMockResp));
|
||||
listRoles = jest.fn().mockReturnValue(makePromise(resps.listRolesMockResp));
|
||||
getCaptchaID = jest
|
||||
.fn()
|
||||
.mockReturnValue(makePromise(resps.getCaptchaIDMockResp));
|
||||
setPreferences = jest
|
||||
.fn()
|
||||
.mockReturnValue(makePromise(resps.setPreferencesMockResp));
|
||||
}
|
||||
|
||||
export const NewMockUsersClient = (url: string): IUsersClient => {
|
||||
return new JestUsersClient(url);
|
||||
};
|
||||
|
|
|
@ -6,8 +6,12 @@ import { FilesPanel } from "../panel_files";
|
|||
import { ICoreState, newState } from "../core_state";
|
||||
import { updater } from "../state_updater";
|
||||
import { MockUsersClient, resps as usersResps } from "../../client/users_mock";
|
||||
import { MockFilesClient, resps as filesResps } from "../../client/files_mock";
|
||||
import {
|
||||
NewMockFilesClient,
|
||||
resps as filesResps,
|
||||
} from "../../client/files_mock";
|
||||
import { MockSettingsClient } from "../../client/settings_mock";
|
||||
import { makePromise } from "../../test/helpers";
|
||||
|
||||
describe("FilesPanel", () => {
|
||||
const initFilesPanel = (): any => {
|
||||
|
@ -15,7 +19,7 @@ describe("FilesPanel", () => {
|
|||
|
||||
const coreState = newState();
|
||||
const usersCl = new MockUsersClient("");
|
||||
const filesCl = new MockFilesClient("");
|
||||
const filesCl = NewMockFilesClient("");
|
||||
const settingsCl = new MockSettingsClient("");
|
||||
|
||||
updater().init(coreState);
|
||||
|
@ -61,16 +65,15 @@ describe("FilesPanel", () => {
|
|||
const newSharingsResp = new Map<string, string>();
|
||||
newSharingsResp.set(sharingDir, "f123456");
|
||||
|
||||
filesCl.setMock({
|
||||
...filesResps,
|
||||
listSharingIDsMockResp: {
|
||||
filesCl.listSharingIDs = jest.fn().mockReturnValueOnce(
|
||||
makePromise({
|
||||
status: 200,
|
||||
statusText: "",
|
||||
data: {
|
||||
IDs: newSharingsResp,
|
||||
},
|
||||
},
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
await filesPanel.addSharing(newSharingPath);
|
||||
|
||||
|
|
|
@ -14,8 +14,8 @@ import (
|
|||
|
||||
"github.com/ihexxa/gocfg"
|
||||
"github.com/ihexxa/quickshare/src/client"
|
||||
fspkg "github.com/ihexxa/quickshare/src/fs"
|
||||
"github.com/ihexxa/quickshare/src/db/userstore"
|
||||
fspkg "github.com/ihexxa/quickshare/src/fs"
|
||||
)
|
||||
|
||||
func startTestServer(config string) *Server {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue