Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lists crud #12

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = {
semi: "error",
"@typescript-eslint/consistent-type-imports": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-array-constructor": "off",
},
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "msw-sp",
"version": "1.10.0",
"version": "1.11.0",
"description": "MSW handlers for mocking SharePoint REST api.",
"main": "lib/index.js",
"engines": {
Expand Down
35 changes: 35 additions & 0 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ export const handlers = (options: Tenant | { tenant: Tenant, delay?: DelayMode |
const title = info.params.title.toString();
return response(await tenantMock.sites.getSite(site).rootWeb.lists.getByTitle(title).rootFolder.get(), info);
}),
...get("/_api/web/defaultDocumentLibrary/items", async (info) => {
const site = info.params.site?.toString() || "/";
return response(await tenantMock.sites.getSite(site).rootWeb.defaultDocumentLibrary.items.get(), info);
}),
...get("/_api/web/lists\\(':listId'\\)/items", async (info) => {
const site = info.params.site?.toString() || "/";
const listId = info.params.listId.toString();
Expand Down Expand Up @@ -323,6 +327,10 @@ export const handlers = (options: Tenant | { tenant: Tenant, delay?: DelayMode |
const site = info.params.site?.toString() || "/";
return response(await tenantMock.sites.getSite(site).rootWeb.defaultDocumentLibrary.rootFolder.get(), info);
}),
...get("/_api/web/defaultDocumentLibrary/rootFolder/files", async (info) => {
const site = info.params.site?.toString() || "/";
return response(await tenantMock.sites.getSite(site).rootWeb.defaultDocumentLibrary.rootFolder.files.get(), info);
}),
...get("/_api/web/defaultDocumentLibrary/getitems", async (info) => {
const site = info.params.site?.toString() || "/";
return response(await tenantMock.sites.getSite(site).rootWeb.defaultDocumentLibrary.items.get(), info);
Expand Down Expand Up @@ -353,6 +361,11 @@ export const handlers = (options: Tenant | { tenant: Tenant, delay?: DelayMode |
const id = info.params.id.toString();
return response(await tenantMock.sites.getSite(site).rootWeb.siteUsers.getById(id).get(), info);
}),
...get("/_api/web/getFileById\\(':id'\\)", async (info) => {
const site = info.params.site?.toString() || "/";
const id = info.params.id.toString();
return response(await tenantMock.sites.getSite(site).rootWeb.getFileById(id).get(), info);
}),
...get("/_api/*", async (info) => {
return response(new Response(undefined, { status: 501, statusText: "Not Implemented (GET)" }), info);
}),
Expand Down Expand Up @@ -383,6 +396,28 @@ export const handlers = (options: Tenant | { tenant: Tenant, delay?: DelayMode |
const payload = await info.request.json();
return response(await tenantMock.sites.getSite(site).rootWeb.lists.getByTitle(title).items.post(payload), info);
}),
...post("/_api/web/lists/getByTitle\\(':title'\\)", async (info) => {
const site = info.params.site?.toString() || "/";
const title = info.params.title.toString();

if (info.request.headers.get("x-http-method") === "DELETE") {
return response(await tenantMock.sites.getSite(site).rootWeb.lists.getByTitle(title).delete(), info);
}

const payload = await info.request.json();
return response(await tenantMock.sites.getSite(site).rootWeb.lists.getByTitle(title).post(payload), info);
}),
...post("/_api/web/lists\\(':listId'\\)", async (info) => {
const site = info.params.site?.toString() || "/";
const listId = info.params.listId.toString();

if (info.request.headers.get("x-http-method") === "DELETE") {
return response(await tenantMock.sites.getSite(site).rootWeb.lists.getById(listId).delete(), info);
}

const payload = await info.request.json();
return response(await tenantMock.sites.getSite(site).rootWeb.lists.getById(listId).post(payload), info);
}),
...post("/_api/web/lists/getByTitle\\(':title'\\)/items\\(:id\\)", async (info) => {
const site = info.params.site?.toString() || "/";
const title = info.params.title.toString();
Expand Down
25 changes: 25 additions & 0 deletions src/mocks/FileMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Utils } from "../Utils.js";
import type { File } from "../types/File.js";

/**
* @internal
*/
export class FileMock {
constructor(private files?: Array<File>, private file?: File) {
}

get = async () => {
if (!this.file) {
return new Response(undefined, { status: 404 });
}

const f = Utils.upperCaseKeys(this.file);
delete f.Content;
f.Exists = true;

return new Response(
JSON.stringify(f),
{ status: 200 },
);
};
}
28 changes: 28 additions & 0 deletions src/mocks/FilesMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { File } from '../types/File.js';
import { FileMock } from './FileMock.js';

/**
* @internal
*/
export class FilesMock {
constructor(private files?: Array<File>) {
}

get = async () => {
if (!this.files) {
return new Response(undefined, { status: 404 });
}

const mocks = this.files.map(file => new FileMock(this.files, file));
const infos = new Array<any>();
for (let i = 0; i !== mocks.length; i++) {
const list = await mocks[i].get();
infos.push(await list.json());
}

return new Response(
JSON.stringify(infos),
{ status: 200 },
);
};
}
5 changes: 5 additions & 0 deletions src/mocks/FolderMock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Utils } from "../Utils.js";
import type { Folder } from "../types/Folder.js";
import { FilesMock } from "./FilesMock.js";

/**
* @internal
Expand All @@ -8,6 +9,10 @@ export class FolderMock {
constructor(private folder: Folder) {
}

public get files() {
return new FilesMock(this.folder.files);
}

get = async () => {
if (!this.folder) {
return new Response(undefined, { status: 404 });
Expand Down
36 changes: 35 additions & 1 deletion src/mocks/ListMock.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DefaultBodyType } from "msw";
import { Utils } from "../Utils.js";
import type { List } from "../types/List.js";
import { BasePermissionsMock } from "./BasePermissionsMock.js";
Expand All @@ -11,7 +12,7 @@ import { ViewsMock } from "./ViewsMock.js";
* @internal
*/
export class ListMock {
constructor(private list?: List) {
constructor(private lists?: Array<List>, private list?: List) {
}

public get fields() {
Expand All @@ -35,6 +36,9 @@ export class ListMock {
}

public get rootFolder() {
if (this.list && "rootFolder" in this.list && this.list.rootFolder) {
return new FolderMock(this.list.rootFolder);
}
return new FolderMock({ name: this.list?.title });
}

Expand Down Expand Up @@ -107,4 +111,34 @@ export class ListMock {
{ status: 200 },
);
};

post = async (payload: DefaultBodyType) => {
if (!this.list) {
return new Response(undefined, { status: 404 });
}

Object.assign(this.list, payload);

return new Response(
JSON.stringify({}),
{ status: 200 },
);
};

delete = async () => {
if (!this.list) {
return new Response(undefined, { status: 404 });
}

const index = this.lists?.findIndex(i => i.id === this.list!.id);
this.list = undefined;
if (index !== undefined) {
this.lists?.splice(index, 1);
}

return new Response(
JSON.stringify({}),
{ status: 200 },
);
};
}
8 changes: 4 additions & 4 deletions src/mocks/ListsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ export class ListsMock {

getByTitle = (title: string) => {
const list = this.lists?.find(list => list.title === title);
return new ListMock(list);
return new ListMock(this.lists, list);
};

getById = (id: string) => {
const list = this.lists?.find(list => list.id === id);
return new ListMock(list);
return new ListMock(this.lists, list);
};

get = async () => {
if (!this.lists) {
return new Response(undefined, { status: 404 });
}

const mocks = this.lists.map(list => new ListMock(list));
const mocks = this.lists.map(list => new ListMock(this.lists, list));
const infos = new Array<any>();
for (let i = 0; i !== mocks.length; i++) {
const list = await mocks[i].get();
Expand All @@ -48,6 +48,6 @@ export class ListsMock {
list.items = [];
this.lists.push(list);

return await new ListMock(list).get();
return await new ListMock(this.lists, list).get();
};
}
44 changes: 42 additions & 2 deletions src/mocks/WebMock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Utils } from "../Utils.js";
import type { Folder } from "../types/Folder.js";
import type { Site } from "../types/Site.js";
import type { Web } from "../types/Web.js";
import { BasePermissionsMock } from "./BasePermissionsMock.js";
import { FileMock } from "./FileMock.js";
import { ListMock } from "./ListMock.js";
import { ListsMock } from "./ListsMock.js";
import { UsersMock } from "./UsersMock.js";
Expand All @@ -28,7 +30,7 @@ export class WebMock {

public get defaultDocumentLibrary() {
const list = this.web.lists?.find(l => l.baseTemplate === 101 && l.isDefaultDocumentLibrary);
return new ListMock(list);
return new ListMock(this.web?.lists, list);
}

public ensureUser(payload: { logonName: string }) {
Expand All @@ -51,7 +53,45 @@ export class WebMock {
return Utils.urls.equals(url, listRelativeUrl);
});

return new ListMock(list);
return new ListMock(this.web?.lists, list);
};

getFileById = (id: string) => {
if (!this.web.lists) {
return new FileMock(undefined, undefined);
}
for (let i = 0; i !== this.web.lists?.length; i++) {
const list = this.web.lists[i];
if (list.baseTemplate !== 101 || !list.rootFolder) {
continue;
}

const getFile = (folder: Folder) => {
if (!folder) {
return undefined;
}
let file = folder.files?.find(f => f.uniqueId === id);
if (file) {
return file;
}
if (folder.folders) {
for (let i = 0; i !== folder.folders?.length; i++) {
file = getFile(folder.folders[i]);
if (file) {
return file;
}
}
}
return undefined;
};

const file = getFile(list.rootFolder);
if (file) {
return new FileMock(list.rootFolder.files, file);
}
}

return new FileMock(undefined, undefined);
};

get = async () => {
Expand Down
Loading