Skip to content

Commit

Permalink
More integration tests (#101)
Browse files Browse the repository at this point in the history
* Add Integration Tests to GetTags, GetPageMedia and GetContentPublishingLimit

* Fix issue with Husky

* Update changelog
  • Loading branch information
TiagoGrosso authored Nov 6, 2022
1 parent 8ef8ac2 commit f2ac0f2
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 56 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run cti && npm run build && npm test && npm run check-format && npm run lint
4 changes: 4 additions & 0 deletions changelog/next_release.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# `next.release`

# Changed

- Fixed issue with parsing `GetContentPublishingLimitRequest` response.
8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"docs": "typedoc --out docs src/Index.ts README.md",
"format": "prettier --write src",
"check-format": "prettier --check src",
"lint": "eslint src"
"lint": "eslint src",
"prepare": "husky install"
},
"keywords": [
"lib",
Expand Down Expand Up @@ -51,11 +52,6 @@
"typedoc": "^0.23.20",
"typescript": "^4.1.5"
},
"husky": {
"hooks": {
"pre-commit": "npm run cti && npm run build && npm test && npm run check-format && npm run lint"
}
},
"directories": {
"test": "test"
},
Expand Down
34 changes: 34 additions & 0 deletions src/it/page/GetContentPublishingLimit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getClient } from '../TestEnv';
import { ContentPublishingLimitFields } from '../../main/Enums';

describe('GetContentPublishingLimit.test', () => {
it('Gets the content publishing limit with all fields', async () => {
const request = getClient().newGetContentPublishingLimitRequest();
const result = await request.execute();
Object.values(ContentPublishingLimitFields).forEach((field) => {
expect(result.getData()[field]).toBeDefined();
});
});

Object.values(ContentPublishingLimitFields).forEach((field) => {
it(`Gets the content publishing limit with only '${field}'`, async () => {
const request = getClient().newGetContentPublishingLimitRequest(field);
const result = await request.execute();
Object.values(ContentPublishingLimitFields)
.filter((expectedField) => field != expectedField)
.forEach((expectedField) => expect(result.getData()[expectedField]).toBeUndefined());
expect(result.getData()[field]).toBeDefined();
});
});

it("Accepts the 'since' param", async () => {
const request = getClient().newGetContentPublishingLimitRequest();
const date = new Date();
date.setHours(date.getHours() - 1);
request.since(date);
const result = await request.execute();
Object.values(ContentPublishingLimitFields).forEach((field) => {
expect(result.getData()[field]).toBeDefined();
});
});
});
32 changes: 32 additions & 0 deletions src/it/page/GetPageInfo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getClient, getPageId } from '../TestEnv';
import { PageField } from '../../main/Enums';

describe('GetPageInfo', () => {
it('Gets the page info with all fields', async () => {
const request = getClient().newGetPageInfoRequest();
const result = await request.execute();
Object.values(PageField).forEach((field) => expect(result.getData()[field]).toBeDefined());
expect(result.getId()).toEqual(getPageId());
});

Object.values(PageField)
.filter((field) => field != PageField.ID)
.forEach((field) => {
it(`Gets the page info with only '${field}'`, async () => {
const request = getClient().newGetPageInfoRequest(field);
const result = await request.execute();
Object.values(PageField)
.filter(
(expectedField) =>
![
field,
PageField.ID, // Always returned
].includes(expectedField)
)
.forEach((expectedField) => expect(result.getData()[expectedField]).toBeUndefined());

expect(result.getId()).toEqual(getPageId());
expect(result.getData()[field]).toBeDefined();
});
});
});
70 changes: 70 additions & 0 deletions src/it/page/GetPageMedia.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { getClient, getPageId } from '../TestEnv';
import { PrivateMediaField, PublicMediaField } from '../../main/Enums';
import { MediaData } from '../../main/requests/data/MediaData';

describe('GetPageMedia', () => {
it('Gets the page media with all fields', async () => {
const request = getClient().newGetPageMediaRequest();
const result = await request.execute();

const data: MediaData[] = result.getData();
expect(data.length).toBeGreaterThan(0);
const firstMedia: MediaData = data[0];
Object.values(PublicMediaField)
.filter(
(field) =>
![
PublicMediaField.CHILDREN, // We don't know if it's a carousel
PublicMediaField.VIDEO_TITLE, // No longer returned
].includes(field)
)
.forEach((field) => expect(firstMedia[field]).toBeDefined());
Object.values(PrivateMediaField)
.filter(
(field) =>
![
PrivateMediaField.THUMBNAIL_URL, // We don't know if it's a video
].includes(field)
)
.forEach((field) => {
expect(firstMedia[field]).toBeDefined();
});
result.getMediaOwnerIds().forEach((id) => expect(id).toEqual(getPageId()));
});

Object.values(PublicMediaField)
.filter(
(field) => ![PublicMediaField.ID, PublicMediaField.CHILDREN, PublicMediaField.VIDEO_TITLE].includes(field)
)
.forEach((field) => {
it(`Gets the page tags with only '${field}'`, async () => {
const request = getClient().newGetTagsRequest(field);
const result = await request.execute();

const data: MediaData[] = result.getData();
expect(data.length).toBeGreaterThan(0);
const firstMedia: MediaData = data[0];
Object.values(PublicMediaField)
.filter(
(expectedField) =>
![
field,
PublicMediaField.ID, // Always returned
].includes(expectedField)
)
.forEach((expectedField) => expect(firstMedia[expectedField]).toBeUndefined());
Object.values(PrivateMediaField)
.filter(
(expectedField) =>
![
field,
PrivateMediaField.THUMBNAIL_URL, // Always returned
].includes(expectedField)
)
.forEach((expectedField) => expect(firstMedia[expectedField]).toBeUndefined());

expect(firstMedia.id).toBeDefined();
expect(firstMedia[field]).toBeDefined();
});
});
});
50 changes: 50 additions & 0 deletions src/it/page/GetTags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { getClient } from '../TestEnv';
import { PublicMediaField } from '../../main/Enums';
import { MediaData } from '../../main/requests/data/MediaData';

describe('GetTags', () => {
it('Gets the page tags with all fields', async () => {
const request = getClient().newGetTagsRequest();
const result = await request.execute();

const data: MediaData[] = result.getData();
expect(data.length).toBeGreaterThan(0);
const firstTag: MediaData = data[0];
Object.values(PublicMediaField)
.filter(
(field) =>
![
PublicMediaField.CHILDREN, // We don't know if it's a carousel
PublicMediaField.VIDEO_TITLE, // No longer returned
].includes(field)
)
.forEach((field) => expect(firstTag[field]).toBeDefined());
});

Object.values(PublicMediaField)
.filter(
(field) => ![PublicMediaField.ID, PublicMediaField.CHILDREN, PublicMediaField.VIDEO_TITLE].includes(field)
)
.forEach((field) => {
it(`Gets the page tags with only '${field}'`, async () => {
const request = getClient().newGetTagsRequest(field);
const result = await request.execute();

const data: MediaData[] = result.getData();
expect(data.length).toBeGreaterThan(0);
const firstTag: MediaData = data[0];
Object.values(PublicMediaField)
.filter(
(expectedField) =>
![
field,
PublicMediaField.ID, // Always returned
].includes(expectedField)
)
.forEach((expectedField) => expect(firstTag[expectedField]).toBeUndefined());

expect(firstTag.id).toBeDefined();
expect(firstTag[field]).toBeDefined();
});
});
});
46 changes: 0 additions & 46 deletions src/it/page/info/GetPageInfo.test.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/main/requests/AbstractGetManyMediaResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class AbstractGetManyMediaResponse extends AbstractResponse<MediaData[]>
}

/**
* Getsan array with the shortcodes of the all the media objects.
* Gets an array with the shortcodes of the all the media objects.
* If a media object does not have the 'shortcode' field, 'undefined' is returned for that object.
*
* @returns an array with the shortcodes of the all the media objects.
Expand Down Expand Up @@ -378,6 +378,8 @@ export class AbstractGetManyMediaResponse extends AbstractResponse<MediaData[]>
* If a media object does not have the 'video_title' field, 'undefined' is returned for that object.
*
* @returns an array with the video titles of the all the media objects.
*
* @deprecated this is no longer returned when using the latest versions of the API.
*/
public getVideoTitles(): (string | undefined)[] {
return this.data.map((elem) => elem.video_title);
Expand All @@ -388,6 +390,8 @@ export class AbstractGetManyMediaResponse extends AbstractResponse<MediaData[]>
* If a media object does have not a the 'video_title' field, 'undefined' is returned for that object.
*
* @returns a map from the id of the media objects to their 'video_title'.
*
* @deprecated this is no longer returned when using the latest versions of the API.
*/
public getVideoTitlesMap(): Map<string, string | undefined> {
return new Map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export class GetContentPublishingLimitRequest extends AbstractRequest<GetContent
* @inheritdoc
*/
protected parseResponse(
response: AxiosResponse<{ data: ContentPublishingLimitData }>
response: AxiosResponse<{ data: ContentPublishingLimitData[] }>
): GetContentPublishingLimitResponse {
return new GetContentPublishingLimitResponse(response.data.data);
return new GetContentPublishingLimitResponse(response.data.data[0]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('GetContentPublishingLimitRequest', () => {

const mock = new MockAdapter(axios);
mock.onGet(`${Constants.API_URL}/${TestConstants.PAGE_ID}/content_publishing_limit`).reply(200, {
data: TestConstants.CONTENT_PUBLISHING_LIMIT_DATA,
data: [TestConstants.CONTENT_PUBLISHING_LIMIT_DATA],
});
it('Parses the response', () => {
expect.assertions(1);
Expand Down

0 comments on commit f2ac0f2

Please sign in to comment.