Skip to content

Commit

Permalink
Merge pull request #8 from allanhvam/dev
Browse files Browse the repository at this point in the history
1.7.0
  • Loading branch information
allanhvam authored Jul 29, 2024
2 parents e8bcbff + d86aa82 commit 4e209ae
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 85 deletions.
181 changes: 106 additions & 75 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "msw-sp",
"version": "1.6.0",
"version": "1.7.0",
"description": "MSW handlers for mocking SharePoint REST api.",
"main": "lib/index.js",
"engines": {
Expand All @@ -9,7 +9,7 @@
"type": "module",
"scripts": {
"build": "npx tsc",
"test": "node --test lib/tests/",
"test": "node --test",
"lint": "npx eslint .",
"watch": "npx tsc --watch"
},
Expand Down
10 changes: 10 additions & 0 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ export const handlers = (options: Tenant | { tenant: Tenant, delay?: DelayMode |
const listRelativeUrl = info.params.listRelativeUrl.toString();
return response(await tenantMock.sites.getSite(site).rootWeb.getList(listRelativeUrl).get(), info);
}),
...get("/_api/web/getList\\(':listRelativeUrl'\\)/forms", async (info) => {
const site = info.params.site?.toString() || "/";
const listRelativeUrl = info.params.listRelativeUrl.toString();
return response(await tenantMock.sites.getSite(site).rootWeb.getList(listRelativeUrl).forms.get(), info);
}),
...get("/_api/web/getList\\(':listRelativeUrl'\\)/fields", async (info) => {
const site = info.params.site?.toString() || "/";
const listRelativeUrl = info.params.listRelativeUrl.toString();
Expand All @@ -264,6 +269,11 @@ export const handlers = (options: Tenant | { tenant: Tenant, delay?: DelayMode |
const listId = info.params.listId.toString();
return response(await tenantMock.sites.getSite(site).rootWeb.lists.getById(listId).items.get(), info);
}),
...get("/_api/web/lists\\(':listId'\\)/forms", async (info) => {
const site = info.params.site?.toString() || "/";
const listId = info.params.listId.toString();
return response(await tenantMock.sites.getSite(site).rootWeb.lists.getById(listId).forms.get(), info);
}),
...get("/_api/web/lists/getByTitle\\(':title'\\)/forms", async (info) => {
const site = info.params.site?.toString() || "/";
const title = info.params.title.toString();
Expand Down
30 changes: 24 additions & 6 deletions src/mocks/ListMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,31 @@ export class ListMock {
public get forms() {
return {
get: async () => {
if (!this.list) {
return new Response(
undefined,
{ status: 404 },
);
}
if (!this.list.forms) {
return new Response(
JSON.stringify([
{
"ServerRelativeUrl": `/${this.list.url || this.list.title}/DispForm.aspx`,
"FormType": 4
},
{
"ServerRelativeUrl": `/${this.list.url || this.list.title}/EditForm.aspx`,
"FormType": 6
}
]),
{ status: 200 },
);
}

return new Response(
JSON.stringify([
{
"ServerRelativeUrl": `/${this.list?.title}/EditForm.aspx`,
"FormType": 6
}
]),
JSON.stringify(this.list.forms.map(Utils.upperCaseKeys)
),
{ status: 200 },
);
}
Expand Down
103 changes: 103 additions & 0 deletions src/tests/forms.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { SPFx, spfi } from "@pnp/sp";
import "@pnp/sp/fields/index.js";
import "@pnp/sp/forms/index.js";
import "@pnp/sp/items/index.js";
import "@pnp/sp/lists/index.js";
import "@pnp/sp/site-users/index.js";
import "@pnp/sp/sites/index.js";
import "@pnp/sp/webs/index.js";
import { setupServer } from 'msw/node';
import * as assert from "node:assert";
import { describe, test } from "node:test";
import { handlers } from '../handlers.js';
import { PageType } from "../types/List.js";

void describe("forms", async () => {
const url = "https://tenant.sharepoint.com";
const server = setupServer(...handlers({
title: "tenant",
url,
sites: {
"forms": {
rootWeb: {
title: "Forms Site",
serverRelativeUrl: "/sites/forms",
lists: [
{
title: "Forms",
baseTemplate: 100,
url: "Lists/Forms",
items: [
],
forms: [
{
formType: PageType.DisplayForm,
serverRelativeUrl: "/sites/forms/Lists/forms/DispForm.aspx",
},
]
},
{
id: "4e319fc6-3da4-4bcb-87fc-67446cf10aa7",
title: "Default",
baseTemplate: 100,
items: [],
url: "lists/default",
}
],
},
},
},
}));
server.listen();

const getContext = (serverRelativeUrl: string) => {
return {
pageContext: {
web: {
absoluteUrl: `${url}${serverRelativeUrl}`,
},
legacyPageContext: {
formDigestTimeoutSeconds: 60,
formDigestValue: "digest",
},
},
};
};

await test("forms", async () => {
const sp = spfi().using(SPFx(getContext("/sites/forms")));

const list = sp.web.getList("/sites/forms/lists/forms");
const forms = await list.forms();

assert.equal(forms.length, 1);
const form = forms[0];

assert.equal(form.FormType, PageType.DisplayForm);
assert.equal(form.ServerRelativeUrl, "/sites/forms/Lists/forms/DispForm.aspx");
});

await test("default", async () => {
const sp = spfi().using(SPFx(getContext("/sites/forms")));

const list = sp.web.lists.getByTitle("Default");
const forms = await list.forms();

assert.ok(forms.length > 0);
const displayForm = forms.find(form => form.FormType === PageType.DisplayForm);

assert.ok(displayForm);
});

await test("default getById", async () => {
const sp = spfi().using(SPFx(getContext("/sites/forms")));

const list = sp.web.lists.getById("4e319fc6-3da4-4bcb-87fc-67446cf10aa7");
const forms = await list.forms();

assert.ok(forms.length > 0);
const displayForm = forms.find(form => form.FormType === PageType.DisplayForm);

assert.ok(displayForm);
});
});
20 changes: 19 additions & 1 deletion src/tests/lists.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SPFx, spfi } from "@pnp/sp";
import "@pnp/sp/fields/index.js";
import "@pnp/sp/items/index.js";
import "@pnp/sp/lists/index.js";
import "@pnp/sp/site-users/index.js";
Expand Down Expand Up @@ -79,7 +80,14 @@ void describe("lists", async () => {
"AuthorId": 1073741822,
"EditorId": 1073741822,
},
]
],
fields: [
{
title: "Id",
internalName: "ID",
typeAsString: "Counter",
},
],
},
],
},
Expand Down Expand Up @@ -163,4 +171,14 @@ void describe("lists", async () => {
assert.equal(list.ContentTypesEnabled, true);
assert.equal(list.NoCrawl, true);
});

await test("fields", async () => {
const sp = spfi().using(SPFx(getContext("/sites/events")));

const eventList = sp.web.lists.getByTitle("Events");
const fields = await eventList.fields.select("InternalName")();
assert.equal(fields.length, 1);
const field = fields[0];
assert.equal(field.InternalName, "ID");
});
});
11 changes: 10 additions & 1 deletion src/types/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type DateTimeField = {
typeAsString: "DateTime";
}

type CounterField = {
typeAsString: "Counter";
}

type LookupField = {
typeAsString: "Lookup";
lookupField: string;
Expand All @@ -24,5 +28,10 @@ export type Field = {
title: string;
description?: string;
internalName: string;
} & (TextField | LookupField | ComputedField | NumberField | DateTimeField);
} & (TextField |
LookupField |
ComputedField |
NumberField |
DateTimeField |
CounterField);

24 changes: 24 additions & 0 deletions src/types/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,32 @@ export type List = {
contentTypes?: Array<ContentType>;
hidden?: boolean;
created?: string;
forms?: Array<Form>;
} & (GenericList | SitePagesList);

export enum PageType {
Invalid = -1,
DefaultView = 0,
NormalView = 1,
DialogView = 2,
View = 3,
DisplayForm = 4,
DisplayFormDialog = 5,
EditForm = 6,
EditFormDialog = 7,
NewForm = 8,
NewFormDialog = 9,
SolutionForm = 10,
PAGE_MAXITEMS = 11
}

export type Form = {
id?: string,
formType?: PageType,
serverRelativeUrl?: string,
decodedUrl?: string,
}

// https://learn.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-wssts/8bf797af-288c-4a1d-a14b-cf5394e636cf
export type GenericList = {
/**
Expand Down

0 comments on commit 4e209ae

Please sign in to comment.