Skip to content

Commit

Permalink
other: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olexandr-mazepa committed Oct 9, 2024
1 parent 777aa84 commit bf05ab6
Show file tree
Hide file tree
Showing 19 changed files with 1,055 additions and 54 deletions.
6 changes: 5 additions & 1 deletion dist/Types/InboxPlacements/InboxPlacements.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export type InboxPlacementsData = {
from: string;
subject: string;
html: string;
provider_filter?: string[];
html?: string;
template_name?: string;
variables?: {
[propName: string]: string;
};
seed_list?: string;
};
export type InboxPlacementsTestResult = {
Expand Down
23 changes: 12 additions & 11 deletions dist/Types/InboxPlacements/Results/InboxPlacementsResults.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { PagesList, ParsedPagesList } from '../../Common';
export type InboxPlacementsResultsQuery = {
sender: string;
subject: string;
provider: string;
target_email: string;
time_after: string | Date;
time_before: string | Date;
cursor: string;
sort: string;
offset: number;
ascending: boolean;
limit: number;
sender?: string;
subject?: string;
provider?: string;
target_email?: string;
time_after?: string | Date;
time_before?: string | Date;
cursor?: string;
sort?: string;
offset?: number;
ascending?: boolean;
limit?: number;
};
export type InboxPlacementsResultsDates = {
time_after?: string;
Expand Down Expand Up @@ -115,6 +115,7 @@ export type InboxPlacementsResult = Omit<InboxPlacementsResultAPIShape, 'Id' | '
created_at: Date;
updated_at: Date;
sharing_expires_at: Date;
id: string;
};
export type InboxPlacementsResultAPIResponse = {
body: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ export type IPRSharingResult = Omit<IPRSharingApiShape, 'expires_at'> & {
expires_at: Date;
};
export type IPRSharingUpdateData = {
enabled: true;
enabled: boolean;
};
export type IPRSharingUpdateAPIResponse = {
body: {
sharing: IPRSharingApiShape;
};
status: number;
};
export type IPRSharingUpdateAPIResponse = IPRSharingAPIResponse;
export type IPRSharingUpdateResult = IPRSharingResult;
2 changes: 1 addition & 1 deletion dist/Types/InboxPlacements/SeedsLists/SeedsLists.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type SeedsListsAPIResponse = {
status: number;
};
export type SeedsListsResult = {
items: SeedList[];
items: SeedList[] | [];
status: number;
pages: ParsedPagesList;
};
Expand Down
35 changes: 27 additions & 8 deletions dist/mailgun.node.js

Large diffs are not rendered by default.

35 changes: 27 additions & 8 deletions dist/mailgun.web.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion lib/Classes/InboxPlacements/AttributesClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export default class InboxPlacementsAttributesClient implements IInboxPlacements

async get(attributeName: string): Promise<InboxPlacementsValuesResult> {
const response = await this.request.get(`${this.path}/${attributeName}`) as InboxPlacementsValuesApiResponse;
return response.body;
return {
...response.body,
status: response.status
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ Consider using string type for property "${key}" to avoid auto-converting`);
const inboxPlacementsResult: InboxPlacementsResult = {
...data,
Box: box,
...handledSeedListDates
...handledSeedListDates,
id: data.Id,
};

delete (inboxPlacementsResult as {Id?: string}).Id;
delete (inboxPlacementsResult as {ID?: string}).ID;

return inboxPlacementsResult;
}
Expand Down
11 changes: 8 additions & 3 deletions lib/Classes/InboxPlacements/SeedsLists/SeedsListsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ Consider using string type for property "${key}" to avoid auto-converting`);
}

protected parseList(response: SeedsListsAPIResponse): SeedsListsResult {
const data = {} as SeedsListsResult;
const data = {
items: []
} as SeedsListsResult;

data.items = response.body.items.map(
data.items = response.body.items?.map(
(item: SeedListAPIShape): SeedList => this.prepareSeedList(item)
);

Expand All @@ -143,7 +145,10 @@ Consider using string type for property "${key}" to avoid auto-converting`);
async list(query: SeedsListsQuery): Promise<SeedsListsResult> {
const queryData = this.prepareQueryData(query);
const response: SeedsListsAPIResponse = await this.request.get('/v4/inbox/seedlists', queryData) as SeedsListsAPIResponse;
return this.parseList(response);
return {
...this.parseList(response),
status: 200
};
}

async get(id: string): Promise<SeedListResult> {
Expand Down
3 changes: 2 additions & 1 deletion lib/Classes/InboxPlacements/inboxPlacements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default class InboxPlacementsClient implements IInboxPlacementsClient {
request: Request;
public seedsLists: ISeedsListsClient;
public results: IInboxPlacementsResultsClient;
providers: IInboxPlacementsProvidersClient
public providers: IInboxPlacementsProvidersClient;

constructor(
request: Request,
seedsListsClient: ISeedsListsClient,
Expand Down
6 changes: 5 additions & 1 deletion lib/Types/InboxPlacements/InboxPlacements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
export type InboxPlacementsData = {
from: string;
subject: string;
html: string;
provider_filter?: string[];
html?: string;
template_name?: string;
variables?: {
[propName: string]: string;
}
seed_list?: string;
}

Expand Down
25 changes: 13 additions & 12 deletions lib/Types/InboxPlacements/Results/InboxPlacementsResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { PagesList, ParsedPagesList } from '../../Common';

/* eslint-disable camelcase */
export type InboxPlacementsResultsQuery = {
sender: string;
subject: string;
provider: string;
target_email: string;
time_after: string | Date;
time_before: string | Date;
cursor: string;
sort: string;
offset: number;
ascending: boolean;
limit: number;
sender?: string;
subject?: string;
provider?: string;
target_email?: string;
time_after?: string | Date;
time_before?: string | Date;
cursor?: string;
sort?: string;
offset?: number;
ascending?: boolean;
limit?: number;
};

export type InboxPlacementsResultsDates = {
Expand Down Expand Up @@ -59,7 +59,7 @@ export type InboxPlacementsBoxAPIShape = {
CreatedTS: number;
tags: string[];
SeedQuality: number;
is_auto_generated: boolean
is_auto_generated: boolean;
};

export type InboxPlacementsBox = Omit<InboxPlacementsBoxAPIShape, 'created_at'| 'updated_at' | 'last_result_at'> & {
Expand Down Expand Up @@ -124,6 +124,7 @@ export type InboxPlacementsResult = Omit<InboxPlacementsResultAPIShape, 'Id' | '
created_at: Date;
updated_at: Date;
sharing_expires_at: Date;
id: string;
};

export type InboxPlacementsResultAPIResponse = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ export type IPRSharingResult = Omit<IPRSharingApiShape, 'expires_at'> & {
};

export type IPRSharingUpdateData = {
enabled: true;
enabled: boolean;
};

export type IPRSharingUpdateAPIResponse = IPRSharingAPIResponse;
export type IPRSharingUpdateAPIResponse = {
body: {
sharing:IPRSharingApiShape
};
status: number;
};
export type IPRSharingUpdateResult = IPRSharingResult;
2 changes: 1 addition & 1 deletion lib/Types/InboxPlacements/SeedsLists/SeedsLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export type SeedsListsAPIResponse = {
};

export type SeedsListsResult = {
items: SeedList[];
items: SeedList[] | [];
status: number;
pages: ParsedPagesList
};
Expand Down
83 changes: 83 additions & 0 deletions test/InboxPlacements.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import formData from 'form-data';

import nock from 'nock';
import Request from '../lib/Classes/common/Request';

import { InputFormData, RequestOptions } from '../lib/Types/Common';

import { IInboxPlacementsClient } from '../lib/Interfaces';
import InboxPlacementsClient from '../lib/Classes/InboxPlacements/inboxPlacements';

import SeedsListsClient from '../lib/Classes/InboxPlacements/SeedsLists/SeedsListsClient';
import InboxPlacementsResultsClient from '../lib/Classes/InboxPlacements/Results/InboxPlacementsResultsClient';
import InboxPlacementsProvidersClient from '../lib/Classes/InboxPlacements/providers/InboxPlacementsProviders';


Check failure on line 15 in test/InboxPlacements.test.ts

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed
// TODO: fix types
describe('Inbox Placements Client', () => {
let client: IInboxPlacementsClient;
let api: nock.Scope;

beforeEach(() => {
const reqObject = new Request({ url: 'https://api.mailgun.net' } as RequestOptions, formData as InputFormData);
client = new InboxPlacementsClient(
reqObject,
{} as SeedsListsClient,
{} as InboxPlacementsResultsClient,
{} as InboxPlacementsProvidersClient
);
api = nock('https://api.mailgun.net');
});

afterEach(() => {
api.done();
});

describe('runTest', () => {
it('runs inbox placements test', async () => {
const providers = [

Check warning on line 38 in test/InboxPlacements.test.ts

View workflow job for this annotation

GitHub Actions / lint

'providers' is assigned a value but never used
{
domain: 'aol.com',
display_name: 'AOL',
region: 'Global',
created_at: '2023-01-04T17:40:03.400Z',
updated_at: '2023-02-17T17:20:12.172Z'
},
{
domain: 'att.net',
display_name: 'AT&T',
region: 'UnitedStates',
created_at: '2023-01-04T17:42:30.576Z',
updated_at: '2023-02-17T17:20:20.664Z'
},
{
domain: 'comcast.net',
display_name: 'Comcast / Xfinity',
region: 'United States',
created_at: '2023-02-16T20:24:10.789Z',
updated_at: '2023-02-16T20:24:10.789Z'
}
];
api.post('/v4/inbox/tests').reply(200, {
result_id: 'test_result_id',
links: {
results: 'test_results'
}
});

const runTestResponse = await client.runTest({
from: 'from',
subject: 'subject',
provider_filter: ['test'],
html: 'some html',
});
runTestResponse.should.eql({
result_id: 'test_result_id',
links: {
results: 'test_results'
},
status: 200
});
});
});
});
70 changes: 70 additions & 0 deletions test/InboxPlacements_Providers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import formData from 'form-data';

import nock from 'nock';
import Request from '../lib/Classes/common/Request';

import { InputFormData, RequestOptions } from '../lib/Types/Common';

import InboxPlacementsProvidersClient from '../lib/Classes/InboxPlacements/providers/InboxPlacementsProviders';
import { IInboxPlacementsProvidersClient } from '../lib/Interfaces/InboxPlacements/providers/InboxPlacementsProviders';

// TODO: fix types
describe('Inbox Placements Providers', () => {
let client: IInboxPlacementsProvidersClient;
let api: nock.Scope;

beforeEach(() => {
const reqObject = new Request({ url: 'https://api.mailgun.net' } as RequestOptions, formData as InputFormData);
client = new InboxPlacementsProvidersClient(reqObject);
api = nock('https://api.mailgun.net');
});

afterEach(() => {
api.done();
});

describe('list', () => {
it('fetches inbox placements providers lists', async () => {
const providers = [
{
domain: 'aol.com',
display_name: 'AOL',
region: 'Global',
created_at: '2023-01-04T17:40:03.400Z',
updated_at: '2023-02-17T17:20:12.172Z'
},
{
domain: 'att.net',
display_name: 'AT&T',
region: 'UnitedStates',
created_at: '2023-01-04T17:42:30.576Z',
updated_at: '2023-02-17T17:20:20.664Z'
},
{
domain: 'comcast.net',
display_name: 'Comcast / Xfinity',
region: 'United States',
created_at: '2023-02-16T20:24:10.789Z',
updated_at: '2023-02-16T20:24:10.789Z'
}
];
api.get('/v4/inbox/providers').reply(200, {
items: providers
});

const IPProviders = await client.list();
IPProviders.should.be.an('object').to.have.property('items');
IPProviders.should.be.an('object').to.have.property('status');
IPProviders.status.should.be.equal(200);

IPProviders.items.should.be.an('array').to.have.property('length').to.equal(3);

const expectedProviders = providers.map((provider) => ({
...provider,
created_at: new Date(provider.created_at),
updated_at: new Date(provider.updated_at)
}));
IPProviders.items.should.eql(expectedProviders);
});
});
});
Loading

0 comments on commit bf05ab6

Please sign in to comment.