Skip to content

Commit

Permalink
feat: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abretonc7s committed Jan 15, 2025
1 parent f9421d9 commit e2e226e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/sdk/src/services/MobilePortStream/write.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer';
import { MAX_MESSAGE_LENGTH } from '../../config';
import { write } from './write';

describe('write function', () => {
Expand Down Expand Up @@ -77,4 +78,50 @@ describe('write function', () => {
new Error('MobilePortStream - disconnected'),
);
});

describe('Message Size Validation', () => {
beforeEach(() => {
jest.clearAllMocks();
global.window = {
location: { href: 'http://example.com' },
ReactNativeWebView: { postMessage: mockPostMessage },
} as any;
});

it('should reject messages exceeding MAX_MESSAGE_LENGTH', () => {
const largeData = {
data: {
jsonrpc: '2.0',
method: 'test_method',
params: ['x'.repeat(MAX_MESSAGE_LENGTH)],
},
};

write(largeData, 'utf-8', cb);

expect(cb).toHaveBeenCalledWith(
expect.objectContaining({
message: expect.stringMatching(
/Message size \d+ exceeds maximum allowed size of \d+ bytes/u,
),
}),
);
expect(mockPostMessage).not.toHaveBeenCalled();
});

it('should accept messages within MAX_MESSAGE_LENGTH', () => {
const validData = {
data: {
jsonrpc: '2.0',
method: 'test_method',
params: ['x'.repeat(100)],
},
};

write(validData, 'utf-8', cb);

expect(cb).toHaveBeenCalledWith();
expect(mockPostMessage).toHaveBeenCalled();
});
});
});

0 comments on commit e2e226e

Please sign in to comment.