Skip to content

Commit

Permalink
test(chat-service): add test case for attachment file (#1396)
Browse files Browse the repository at this point in the history
add test case of has many relation for attachment file

GH-1395
  • Loading branch information
Surbhi-sharma1 committed May 11, 2023
1 parent ddfef35 commit f4373d4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
7 changes: 6 additions & 1 deletion services/chat-service/src/__tests__/acceptance/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createRestAppClient,
givenHttpServerConfig,
} from '@loopback/testlab';
import {AuthenticationBindings} from 'loopback4-authentication';
import {ChatApplication} from '../application';

export async function setUpApplication(): Promise<AppWithClient> {
Expand All @@ -23,7 +24,11 @@ export async function setUpApplication(): Promise<AppWithClient> {
await app.start();

const client = createRestAppClient(app);

app.bind(AuthenticationBindings.CURRENT_USER).to({
id: 1,
username: 'test_user',
password: 'test_password',
});
return {app, client};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {expect} from '@loopback/testlab';
import {setUpApplication} from '../acceptance/helper';
import {ChatApplication} from '../application';
import {MessageRepository} from '../../repositories';
describe('supports @has many', () => {
let app: ChatApplication;
let messageRepo: MessageRepository;
const messageObj = {
body: 'Test message',
channelId: 'test channel',
channelType: 'testchannel type',
};
before('setupApplication', async () => {
({app} = await setUpApplication());
});
after(async () => {
await app.stop();
});
before(givenRepositories);
afterEach(deleteMockData);
it('fetch messages with associated message file', async () => {
const message = await messageRepo.create(messageObj);
const file1 = await messageRepo.attachmentFiles(message.id).create({
fileKey: 'test_file1.txt',
});
const file2 = await messageRepo.attachmentFiles(message.id).create({
fileKey: 'test_file2.txt',
});
const filter = {
where: {id: message.id},
include: [{relation: 'attachmentFiles'}],
};
const messagesWithFiles = await messageRepo.find(filter);
const messageFiles = messagesWithFiles[0].attachmentFiles;
expect(messagesWithFiles[0].id).to.equal(message.id);
expect(messageFiles[0].fileKey).to.equal(file1.fileKey);
expect(messageFiles[1].fileKey).to.equal(file2.fileKey);
});
async function givenRepositories() {
messageRepo = await app.getRepository(MessageRepository);
}
async function deleteMockData() {
await messageRepo.deleteAllHard();
}
});

0 comments on commit f4373d4

Please sign in to comment.