Skip to content

Commit

Permalink
feat(join): new joinGroup and leaveGroup methods
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-groups
  • Loading branch information
jgravois committed Mar 26, 2019
1 parent b1a935b commit fa604de
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/arcgis-rest-groups/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./protect";
export * from "./remove";
export * from "./search";
export * from "./update";
export * from "./join";
60 changes: 60 additions & 0 deletions packages/arcgis-rest-groups/src/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { request, getPortalUrl } from "@esri/arcgis-rest-request";

import { IGroupIdRequestOptions } from "./helpers";

/**
* ```js
* import { joinGroup } from '@esri/arcgis-rest-groups';
* //
* joinGroup({
* id: groupId,
* authentication
* })
* .then(response)
* ```
* Join a Group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/join-group.htm) for more information.
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the success/failure status of the request
*/
export function joinGroup(
requestOptions: IGroupIdRequestOptions
): Promise<{ success: boolean; groupId: string }> {
const url = `${getPortalUrl(requestOptions)}/community/groups/${
requestOptions.id
}/join`;
const options: IGroupIdRequestOptions = {
...requestOptions
};
return request(url, options);
}

/**
* ```js
* import { joinGroup } from '@esri/arcgis-rest-groups';
* //
* joinGroup({
* id: groupId,
* authentication
* })
* .then(response)
* ```
* Leave a Group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/leave-group.htm) for more information.
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the success/failure status of the request
*/
export function leaveGroup(
requestOptions: IGroupIdRequestOptions
): Promise<{ success: boolean; groupId: string }> {
const url = `${getPortalUrl(requestOptions)}/community/groups/${
requestOptions.id
}/leave`;
const options: IGroupIdRequestOptions = {
...requestOptions
};
return request(url, options);
}
15 changes: 12 additions & 3 deletions packages/arcgis-rest-groups/src/protect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { IGroupIdRequestOptions } from "./helpers";
*/
export function protectGroup(
requestOptions: IGroupIdRequestOptions
): Promise<any> {
): Promise<{ success: boolean }> {
const url = `${getPortalUrl(requestOptions)}/community/groups/${
requestOptions.id
}/protect`;
Expand All @@ -33,13 +33,22 @@ export function protectGroup(
}

/**
* Unprotect a Group.
* ```js
* import { unprotectGroup } from '@esri/arcgis-rest-groups';
* //
* unprotectGroup({
* id: groupId,
* authentication
* })
* .then(response)
* ```
* Unprotect a Group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/unprotect-group.htm) for more information.
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the success/failure status of the request
*/
export function unprotectGroup(
requestOptions: IGroupIdRequestOptions
): Promise<any> {
): Promise<{ success: boolean }> {
const url = `${getPortalUrl(requestOptions)}/community/groups/${
requestOptions.id
}/unprotect`;
Expand Down
64 changes: 64 additions & 0 deletions packages/arcgis-rest-groups/test/join.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { joinGroup, leaveGroup } from "../src/join";

import { GroupEditResponse } from "./mocks/responses";

import { encodeParam } from "@esri/arcgis-rest-request";
import * as fetchMock from "fetch-mock";

describe("groups", () => {
afterEach(fetchMock.restore);

describe("authenticted methods", () => {
const MOCK_AUTH = {
getToken() {
return Promise.resolve("fake-token");
},
portal: "https://myorg.maps.arcgis.com/sharing/rest"
};
const MOCK_REQOPTS = {
authentication: MOCK_AUTH
};

it("should help a user join a group", done => {
fetchMock.once("*", GroupEditResponse);

joinGroup({ id: "5bc", ...MOCK_REQOPTS })
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/community/groups/5bc/join"
);
expect(options.method).toBe("POST");
expect(options.body).toContain(encodeParam("f", "json"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
done();
})
.catch(e => {
fail(e);
});
});
it("should help a user leave a group", done => {
fetchMock.once("*", GroupEditResponse);

leaveGroup({ id: "5bc", ...MOCK_REQOPTS })
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/community/groups/5bc/leave"
);
expect(options.method).toBe("POST");
expect(options.body).toContain(encodeParam("f", "json"));
expect(options.body).toContain(encodeParam("token", "fake-token"));
done();
})
.catch(e => {
fail(e);
});
});
});
});

0 comments on commit fa604de

Please sign in to comment.