Skip to content

Commit

Permalink
Refactor and List-Item Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcameron1231 committed Oct 13, 2023
1 parent fcb2fac commit 8fd18a4
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 82 deletions.
22 changes: 16 additions & 6 deletions docs/graph/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,34 @@ var newItem = await graph.sites.getById("{site identifier}").lists.getById("{lis
import { graphfi } from "@pnp/graph";
import "@pnp/graph/list-items";
import "@pnp/graph/lists";
import "@pnp/graph/documentSetVersions";

const graph = graphfi(...);
var item = await graph.sites.getById("{site identifier}").lists.getById("{list identifier}").items.getById("{item identifier}")();
var item = await graph.sites.getById("{site identifier}").lists.getById("{list identifier}").items.getById("{item identifier}").documentSetVersions();
var documentSetVersions = item.documentSetVersions();

```

### Get Document Set Versions By Id

```TypeScript
import { graphfi } from "@pnp/graph";
import "@pnp/graph/list-items";
import "@pnp/graph/lists";

const graph = graphfi(...);
var documentSetVersion = await graph.sites.getById("{site identifier}").lists.getById("{list identifier}").items.getById("{item identifier}").documentSetVersions.getById("{document set version id}");

```

### Create a new Document Set Version

```TypeScript
import { graphfi } from "@pnp/graph";
import "@pnp/graph/list-items";
import "@pnp/graph/lists";
import "@pnp/graph/documentSetVersions";

const graph = graphfi(...);
var item = await graph.sites.getById("{site identifier}").lists.getById("{list identifier}").items.getById("{item identifier}").documentSetVersions.add("New Comment");
var version = await graph.sites.getById("{site identifier}").lists.getById("{list identifier}").items.getById("{item identifier}").documentSetVersions.add("New Comment");

```

Expand All @@ -109,10 +120,9 @@ var item = await graph.sites.getById("{site identifier}").lists.getById("{list i
import { graphfi } from "@pnp/graph";
import "@pnp/graph/list-items";
import "@pnp/graph/lists";
import "@pnp/graph/documentSetVersions";

const graph = graphfi(...);
var item = await graph.sites.getById("{site identifier}").lists.getById("{list identifier}").items.getById("{item identifier}").documentSetVersions.getById(1).restore();
await graph.sites.getById("{site identifier}").lists.getById("{list identifier}").items.getById("{item identifier}").documentSetVersions.getById("{document set version id}").restore();

```

Expand Down
10 changes: 0 additions & 10 deletions packages/graph/documentSetVersions/index.ts

This file was deleted.

60 changes: 0 additions & 60 deletions packages/graph/documentSetVersions/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { addProp } from "@pnp/queryable";
import { DocumentSetVersions, IDocumentSetVersions } from "./types.js";
import { _ListItem } from "../list-item/types.js";
import { DocumentSetVersions, _ListItem } from "./types.js";

declare module "../list-item/types" {
declare module "./types" {
interface _ListItem {
readonly documentSetVersions: IDocumentSetVersions;
}
Expand All @@ -11,4 +10,4 @@ declare module "../list-item/types" {
}
}

addProp(_ListItem, "documentSetVersions", DocumentSetVersions);
addProp(_ListItem, "documentSetVersions", DocumentSetVersions);
6 changes: 6 additions & 0 deletions packages/graph/list-item/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import "./list.js";
import "./document-sets.js";

export {
ListItems,
IListItems,
ListItem,
IListItem,
IListItemAddResult,
IDocumentSetVersion,
DocumentSetVersion,
DocumentSetVersions,
IDocumentSetVersions,
IDocumentSetVersionAddResult,
} from "./types.js";
60 changes: 58 additions & 2 deletions packages/graph/list-item/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ListItem as IListItemEntity, ListItemVersion as IListItemVersion } from "@microsoft/microsoft-graph-types";
import { ListItem as IListItemEntity, ListItemVersion as IListItemVersion, DocumentSetVersion as IDocumentSetVersionEntity } from "@microsoft/microsoft-graph-types";
import { _GraphCollection, graphInvokableFactory, _GraphInstance, IGraphCollection, GraphCollection, graphPost } from "../graphqueryable.js";
import { defaultPath, deleteable, IDeleteable, updateable, IUpdateable, getById, IGetById } from "../decorators.js";
import { body } from "@pnp/queryable";
Expand All @@ -17,7 +17,7 @@ export class _ListItem extends _GraphInstance<IListItemEntity> {
return <any>GraphCollection(this, "versions");
}
}
export interface IListItem extends _ListItem, IDeleteable, IUpdateable { }
export interface IListItem extends _ListItem, IDeleteable, IUpdateable {}
export const ListItem = graphInvokableFactory<IListItem>(_ListItem);

/**
Expand Down Expand Up @@ -45,6 +45,62 @@ export class _ListItems extends _GraphCollection<IListItemEntity[]>{
export interface IListItems extends _ListItems, IGetById<IListItem> { }
export const ListItems = graphInvokableFactory<IListItems>(_ListItems);

/**
* Represents a document set version
*/
@deleteable()
export class _DocumentSetVersion extends _GraphInstance<IDocumentSetVersionEntity> {
/**
* Restore a document set version
*
*/
public async restore(): Promise<void> {
return graphPost(DocumentSetVersion(this, "restore"));
}
}
export interface IDocumentSetVersion extends _DocumentSetVersion, IDeleteable { }
export const DocumentSetVersion = graphInvokableFactory<IDocumentSetVersion>(_DocumentSetVersion);

/**
* Describes a collection of document set versions
*
*/
@defaultPath("documentSetVersions")
@getById(DocumentSetVersion)
export class _DocumentSetVersions extends _GraphCollection<IDocumentSetVersionEntity[]>{
/**
* Create a new document set version as specified in the request body.
*
* @param comment a comment about the captured version
* @param shouldCaptureMinorVersion If true, minor versions of items are also captured; otherwise, only major versions will be captured.
*
*/
public async add(comment: string, shouldCaptureMinorVersion = false): Promise<IDocumentSetVersionAddResult> {

const postBody = {
comment: comment,
shouldCaptureMinorVersion: shouldCaptureMinorVersion,
};
const data = await graphPost(this, body(postBody));

return {
data,
item: (<any>this).getById(data.id),
};
}
}

export interface IDocumentSetVersions extends _DocumentSetVersions, IGetById<IDocumentSetVersion> {}
export const DocumentSetVersions = graphInvokableFactory<IDocumentSetVersions>(_DocumentSetVersions);

/**
* IListAddResult
*/
export interface IDocumentSetVersionAddResult {
item: IDocumentSetVersion;
data: IDocumentSetVersionEntity;
}

/**
* IListAddResult
*/
Expand Down
1 change: 1 addition & 0 deletions settings.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const settings = {
// set your scopes as needed here
scopes: ["https://{tenant}.sharepoint.com/.default"]
},
testWebUrl:"{ site collection for testing }"
},
// all are optional if using msal
graph: {
Expand Down
133 changes: 133 additions & 0 deletions test/graph/list-items.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { expect } from "chai";
import "@pnp/graph/sites";
import "@pnp/graph/lists";
import { List } from "@microsoft/microsoft-graph-types";
import { ISite } from "@pnp/graph/sites";
import { getRandomString } from "@pnp/core";
import getTestingGraphSPSite from "./utilities/getTestingGraphSPSite.js";
import { pnpTest } from "../pnp-test.js";
import { IList } from "@pnp/graph/lists";
import { IListItem } from "@pnp/graph/list-item/types.js";

describe("List-Items", function () {
let site: ISite;
let list: IList;
let item: IListItem;

const sampleList: List = {
displayName: "PnPGraphTestList",
list: { "template": "ItemTestList-Graph" },
};

before(async function () {

if (!this.pnp.settings.enableWebTests) {
this.skip();
}

site = await getTestingGraphSPSite(this);

const props = await this.props({
displayName: getRandomString(5) + "Add",
});

const listTemplate = JSON.parse(JSON.stringify(sampleList));
listTemplate.displayName += props.displayName;
const list = (await site.lists.add(listTemplate)).list;

// add test items. Document set can be added later
if(list){
await list.items.add({Title: `Item ${getRandomString(4)}`} as any);
await list.items.add({Title: `Item ${getRandomString(4)}`} as any);
// can't do until Graph Drives is done.
/* const documentSetCT = await site.contentTypes.getById("0x0120D520")();
await list.contentTypes.add(documentSetCT);
// create item
const itemData = await list.items.select("Id").top(1)<{ Id: number }[]>();
item = list.items.getById(itemData[0].Id?.toString());
// add document set version to item
item.documentSetVersions.add("Test");
*/
}

});

it("items", pnpTest("3e0e16a0-5683-4c3a-aa3d-f35bb6912de1", async function () {
const items = await list.items();
return expect(items).to.be.an("array") && expect(items[0]).to.haveOwnProperty("id");
}));

it("getById()", pnpTest("6f9592fd-1568-4d9c-a3f5-7f45165d84f2", async function () {
const itemData = await list.items.select("Id").top(1)<{ Id: number }[]>();
return expect(itemData[0].Id).is.not.null;
}));

it("add", pnpTest("587e280b-0342-4515-a166-1b05cee9f242", async function () {
// fieldvalueset. ugh. Casting as any.
const itemAdded = await list.items.add({fields:
{
title: getRandomString(5) + "Add",
},
} as any);

return expect((itemAdded.data.id)).is.not.null;
}));

it("update", pnpTest("5766613a-51b8-4f88-ba0f-2436d160b86b", async function () {
// fieldvalueset. ugh. Casting as any.
const itemUpdated = await item.update({fields:
{
title: getRandomString(5) + "Update",
},
} as any);


return expect(itemUpdated).is.not.null;
}));

it("delete", pnpTest("e55bf53f-1316-4e47-97c1-b0c0cdd860ef", async function () {
const item = await list.items.add({fields:
{
title: getRandomString(5) + "Add",
},
} as any);
const r = await list.items.filter(`Id eq '${item.data.id}'`)();
return expect(r.length).to.eq(0);
}));

it.skip("documentSetVersions", pnpTest("c2889ca3-0230-4c6e-879d-71cc9cd08e83", async function () {
const versions = await item.documentSetVersions();
return expect(versions).to.be.an("array") && expect(versions[0]).to.haveOwnProperty("id");
}));

it.skip("documentSetVersions - getById()", pnpTest("35226d93-204b-4877-9041-26e04e437914", async function () {
const versions = await item.documentSetVersions();

const version = await item.documentSetVersions.getById(versions[0].id);
return expect(version).to.not.be.null && expect(version).to.haveOwnProperty("id");
}));

it.skip("documentSetVersions - add()", pnpTest("a192e096-fe84-4c2c-adc5-b1b9021c0031", async function () {
const documentSetVersion = await item.documentSetVersions.add("New Comment");
return expect(documentSetVersion).to.not.be.null && expect(documentSetVersion).to.haveOwnProperty("id");
}));

it.skip("documentSetVersions - restore()", pnpTest("8814b247-4087-4c87-9a8f-af997f7d8745", async function () {
const restore = await item.documentSetVersions[0].restore();
return expect(restore).to.be.fulfilled;
}));

// Remove the test list we created
after(async function () {
if (list) {
try {
await list.delete();
} catch (err) {
console.error("Cannot clean up test list");
}
}
return;
});

});

0 comments on commit 8fd18a4

Please sign in to comment.