Skip to content

Commit

Permalink
extend mockMessageFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNi245 committed Sep 24, 2024
1 parent b3446fe commit e3bd93d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/lib/test-helper/mocks/mockMessageFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { addPostmark } from '@dm3-org/dm3-lib-delivery';
import {
Message,
MessageState,
MessageType,
SendDependencies,
buildEnvelop,
} from '@dm3-org/dm3-lib-messaging';
Expand Down Expand Up @@ -115,14 +116,17 @@ export const MockMessageFactory = (
const createStorageEnvelopContainer = async (
msg: string,
messageState: MessageState = MessageState.Created,
type: MessageType = 'NEW',
referenceMessageHash: string = '',
) => {
const message: Message = {
message: msg,
metadata: {
to: receiver.account.ensName,
from: sender.account.ensName,
timestamp: Date.now(),
type: 'NEW',
referenceMessageHash,
type,
},
signature: '',
};
Expand Down Expand Up @@ -155,6 +159,23 @@ export const MockMessageFactory = (
throw err;
}
};
const createMessageModel = async (
msg: string,
type: MessageType,
referenceMessageHash: string = '',
) => {
const container = await createStorageEnvelopContainer(
msg,
MessageState.Created,
type,
referenceMessageHash,
);
return {
...container,
reactions: [],
source: 0,
};
};

const createMessage = async (msg: string) => {
const message: Message = {
Expand All @@ -176,5 +197,6 @@ export const MockMessageFactory = (
createMessage,
createEnvelop,
createStorageEnvelopContainer,
createMessageModel,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
getMockDeliveryServiceProfile,
MockDeliveryServiceProfile,
MockedUserProfile,
MockMessageFactory,
mockUserProfile,
} from '@dm3-org/dm3-lib-test-helper';
import { ethers } from 'ethers';
import { renderMessage } from './renderMessage';

describe('Render Message', () => {
let sender: MockedUserProfile;
let receiver: MockedUserProfile;
let ds1: MockDeliveryServiceProfile;

beforeEach(async () => {
sender = await mockUserProfile(
ethers.Wallet.createRandom(),
'alice.eth',
['ds1.eth'],
);
receiver = await mockUserProfile(
ethers.Wallet.createRandom(),
'bob.eth',
['ds1.eth', 'ds2.eth'],
);
ds1 = await getMockDeliveryServiceProfile(
ethers.Wallet.createRandom(),
'http://ds1.api',
);
});
describe('edit message', () => {
it('NEW EDIT', async () => {
const mockMessageFactory = MockMessageFactory(
sender,
receiver,
ds1,
);

const ogMessage = await mockMessageFactory.createMessageModel(
'Hello Dm3',
'NEW',
);

const editMessage = await mockMessageFactory.createMessageModel(
'Hello Dm3 Edit',
'EDIT',
ogMessage.envelop.metadata?.encryptedMessageHash,
);

const messages = [ogMessage, editMessage];

const res = renderMessage(messages);

console.log(res[0].envelop);

expect(res.length).toBe(1);
expect(res[0].envelop.message.message).toBe('Hello Dm3 Edit');
});
});
});

0 comments on commit e3bd93d

Please sign in to comment.