Skip to content

Commit

Permalink
added missed unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Shoaibdev7 committed Feb 6, 2025
1 parent 434ac26 commit 1959a4a
Showing 1 changed file with 186 additions and 0 deletions.
186 changes: 186 additions & 0 deletions src/__test__/postAllUsersToTribe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, it, expect, beforeEach } from '@jest/globals';
import { waitFor } from '@testing-library/react';

global.fetch = jest.fn();

Expand Down Expand Up @@ -191,4 +192,189 @@ describe('postAllUsersToTribe', () => {

expect(calls).toEqual([1, 2]);
});

it('Single Node with Valid Data', async () => {
const mockFetch = jest.fn().mockResolvedValue({ ok: true });
global.fetch = mockFetch;

const singleNode = [
{
external_ip: 'http://valid-ip',
alias: 'testAlias',
authToken: 'validToken'
}
];

await postAllUsersToTribe(singleNode);

expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(
'http://valid-ip/profile',
expect.objectContaining({
method: 'POST',
body: expect.stringContaining('testAlias'),
headers: { 'x-user-token': 'validToken' }
})
);
});

it('Node with Empty Alias', async () => {
const mockFetch = jest.fn().mockResolvedValue({ ok: true });
global.fetch = mockFetch;

const nodeWithEmptyAlias = [
{
external_ip: 'http://valid-ip',
alias: '',
authToken: 'validToken'
}
];

await postAllUsersToTribe(nodeWithEmptyAlias);

expect(mockFetch).toHaveBeenCalledWith(
'http://valid-ip/profile',
expect.objectContaining({
body: expect.stringContaining('"owner_alias":""')
})
);
});

it('Node with Empty AuthToken', async () => {
const mockFetch = jest.fn().mockResolvedValue({ ok: true });
global.fetch = mockFetch;

const nodeWithEmptyToken = [
{
external_ip: 'http://valid-ip',
alias: 'testAlias',
authToken: ''
}
];

await postAllUsersToTribe(nodeWithEmptyToken);

expect(mockFetch).toHaveBeenCalledWith(
'http://valid-ip/profile',
expect.objectContaining({
headers: { 'x-user-token': '' }
})
);
});

it('Null Node Properties', async () => {
const mockFetch = jest.fn().mockResolvedValue({ ok: true });
global.fetch = mockFetch;

const nodeWithNullProps = [
{
external_ip: null,
alias: null,
authToken: null
}
];

await postAllUsersToTribe(nodeWithNullProps);
waitFor(() => {
expect(mockFetch).toHaveBeenCalledWith(
'null/profile',
expect.objectContaining({
body: expect.stringContaining('"owner_alias":null'),
headers: { 'x-user-token': 'null' }
})
);
});
});

it('Non-String Node Properties', async () => {
const mockFetch = jest.fn().mockResolvedValue({ ok: true });
global.fetch = mockFetch;

const nodeWithNonStringProps = [
{
external_ip: 123,
alias: 456,
authToken: 789
}
];

await postAllUsersToTribe(nodeWithNonStringProps);

expect(mockFetch).toHaveBeenCalledWith(
'123/profile',
expect.objectContaining({
body: expect.stringContaining('"owner_alias":"456"'),
headers: { 'x-user-token': '789' }
})
);
});

it('Node with Long Alias', async () => {
const mockFetch = jest.fn().mockResolvedValue({ ok: true });
global.fetch = mockFetch;

const longAlias = 'a'.repeat(1000);
const nodeWithLongAlias = [
{
external_ip: 'http://valid-ip',
alias: longAlias,
authToken: 'validToken'
}
];

await postAllUsersToTribe(nodeWithLongAlias);

expect(mockFetch).toHaveBeenCalledWith(
'http://valid-ip/profile',
expect.objectContaining({
body: expect.stringContaining(longAlias)
})
);
});

it('Node with Special Characters in AuthToken', async () => {
const mockFetch = jest.fn().mockResolvedValue({ ok: true });
global.fetch = mockFetch;

const specialAuthToken = '!@#$%^&*()+=-[]{}|;:,.<>?';
const nodeWithSpecialAuthToken = [
{
external_ip: 'http://valid-ip',
alias: 'testAlias',
authToken: specialAuthToken
}
];

await postAllUsersToTribe(nodeWithSpecialAuthToken);

expect(mockFetch).toHaveBeenCalledWith(
'http://valid-ip/profile',
expect.objectContaining({
headers: { 'x-user-token': specialAuthToken }
})
);
});

it('Node with Long AuthToken', async () => {
const mockFetch = jest.fn().mockResolvedValue({ ok: true });
global.fetch = mockFetch;

const longAuthToken = 'a'.repeat(1000);
const nodeWithLongAuthToken = [
{
external_ip: 'http://valid-ip',
alias: 'testAlias',
authToken: longAuthToken
}
];

await postAllUsersToTribe(nodeWithLongAuthToken);

expect(mockFetch).toHaveBeenCalledWith(
'http://valid-ip/profile',
expect.objectContaining({
headers: { 'x-user-token': longAuthToken }
})
);
});
});

0 comments on commit 1959a4a

Please sign in to comment.