Skip to content

Commit

Permalink
Support Email/changes method (linagora#52)
Browse files Browse the repository at this point in the history
Support Email/changes method
  • Loading branch information
alagane authored Apr 29, 2021
1 parent d6882b5 commit a89e845
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
IEmailSubmissionChangesArguments,
IEmailSubmissionSetResponse,
IEmailSubmissionChangesResponse,
IEmailChangesArguments,
IEmailChangesResponse,
} from './types';

export class Client {
Expand Down Expand Up @@ -106,6 +108,10 @@ export class Client {
return this.request<IEmailGetResponse>('Email/get', args);
}

public email_changes(args: IEmailChangesArguments): Promise<IEmailChangesResponse> {
return this.request<IEmailChangesResponse>('Email/changes', args);
}

public email_query(args: IEmailQueryArguments): Promise<IEmailQueryResponse> {
return this.request<IEmailQueryResponse>('Email/query', args);
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type IMethodName =
| 'Mailbox/changes'
| 'Mailbox/set'
| 'Email/get'
| 'Email/changes'
| 'Email/query'
| 'Email/set'
| 'EmailSubmission/get'
Expand Down Expand Up @@ -379,6 +380,8 @@ export interface IMailboxEmailList {
deleted: Date | null;
}

export type IEmailChangesArguments = IChangesArguments;

export type IEmailChangesResponse = IChangesResponse;

export type IThreadChangesResponse = IChangesResponse;
Expand Down
61 changes: 60 additions & 1 deletion tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GenericContainer, StartedTestContainer } from 'testcontainers';
import { Client } from '../src/index';
import { AxiosTransport } from '../src/utils/axios-transport';
import axios from 'axios';
import { IMailboxChangesResponse } from '../src/types';
import { IEmailChangesResponse, IMailboxChangesResponse } from '../src/types';

describe('jmap-client-ts', () => {
const DEFAULT_TIMEOUT = 60000;
Expand Down Expand Up @@ -108,6 +108,65 @@ describe('jmap-client-ts', () => {
expect(emailGetResponse.accountId).toBeDefined();
});

it('should have email_changes working', async () => {
const getResponse = await client.email_get({
accountId: client.getAccountIds()[0],
ids: [],
});

const changesResponse = await client.email_changes({
accountId: client.getAccountIds()[0],
sinceState: getResponse.state,
});

expect(changesResponse).toMatchObject<IEmailChangesResponse>({
accountId: client.getAccountIds()[0],
oldState: getResponse.state,
newState: getResponse.state,
hasMoreChanges: false,
created: [],
updated: [],
destroyed: [],
});

const getMailboxesResponse = await client.mailbox_get({
accountId: client.getAccountIds()[0],
ids: null,
});

const draftMailboxId = <string>(
getMailboxesResponse.list.find(mailbox => mailbox.name.toLowerCase() === 'drafts')?.id
);

const emailCreatedResponse = await client.email_set({
accountId: client.getAccountIds()[0],
create: {
emailToCreateId: {
mailboxIds: {
[draftMailboxId]: true,
},
},
},
});

const emailCreatedId = emailCreatedResponse.created?.emailToCreateId.id as string;

const newChangesResponse = await client.email_changes({
accountId: client.getAccountIds()[0],
sinceState: getResponse.state,
});

expect(newChangesResponse).toMatchObject<IEmailChangesResponse>({
accountId: client.getAccountIds()[0],
oldState: getResponse.state,
newState: emailCreatedResponse.newState,
hasMoreChanges: false,
created: [emailCreatedId],
updated: [],
destroyed: [],
});
});

it('should have email_set working', async () => {
const getMailboxesResponse = await client.mailbox_get({
accountId: client.getAccountIds()[0],
Expand Down
10 changes: 4 additions & 6 deletions tests/unit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Client } from '../src';
import { AxiosTransport } from '../src/utils/axios-transport';
import axios from 'axios';

describe('jmap-client-ts unittests', () => {
const axiosTransport = new AxiosTransport(axios);
const mockTransportGet = jest.fn();
const mockTransportPost = jest.fn();
const mockTransport = { get: mockTransportGet, post: mockTransportPost };

const accountId = '123456789abcdef';

axiosTransport.get = mockTransportGet;
axiosTransport.post = mockTransportPost;
mockTransport.get = mockTransportGet;
mockTransport.post = mockTransportPost;

const client = new Client({
sessionUrl: 'session-url',
accessToken: '',
httpHeaders: {},
overriddenApiUrl: 'api-url',
transport: axiosTransport,
transport: mockTransport,
});

beforeEach(async () => {
Expand Down

0 comments on commit a89e845

Please sign in to comment.