Skip to content

Commit

Permalink
Merge pull request #163 from Telegram-Mini-Apps/157-add-missing-init-…
Browse files Browse the repository at this point in the history
…data-parameters

157 add missing init data parameters
  • Loading branch information
heyqbnk authored Nov 4, 2023
2 parents fc29e2d + 9d3956a commit e3416e3
Show file tree
Hide file tree
Showing 6 changed files with 478 additions and 87 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-jokes-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tma.js/sdk": patch
---

Fix the type of InitData.chatInstance property
106 changes: 106 additions & 0 deletions packages/init-data/tests/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { describe, expect, it } from 'vitest';

import { chat } from '../src/index.js';

describe('chat.ts', () => {
describe('chat', () => {
describe('id', () => {
it('should throw an error in case, this property is missing', () => {
expect(
() => chat().parse({
type: 'group chat',
title: 'My chat',
}),
).toThrow();
});

it('should parse source property as number and pass it to the "id" property', () => {
expect(
chat().parse({
id: 882,
type: 'group chat',
title: 'My chat',
}),
).toMatchObject({
id: 882,
});
});
});

describe('type', () => {
it('should throw an error in case, this property is missing', () => {
expect(
() => chat().parse({
id: 223,
title: 'My chat',
}),
).toThrow();
});

it('should parse source property as number and pass it to the "type" property', () => {
expect(
chat().parse({
id: 882,
type: 'group chat',
title: 'My chat',
}),
).toMatchObject({
type: 'group chat',
});
});
});

describe('title', () => {
it('should throw an error in case, this property is missing', () => {
expect(
() => chat().parse({
id: 223,
type: 'group chat',
}),
).toThrow();
});

it('should parse source property as number and pass it to the "title" property', () => {
expect(
chat().parse({
id: 882,
type: 'group chat',
title: 'My chat',
}),
).toMatchObject({
title: 'My chat',
});
});
});

describe('photo_url', () => {
it('should parse source property as number and pass it to the "photoUrl" property', () => {
expect(
chat().parse({
id: 882,
type: 'group chat',
title: 'My chat',
photo_url: 'https://image.com',
}),
).toMatchObject({
photoUrl: 'https://image.com',
});
});
});

describe('username', () => {
it('should parse source property as number and pass it to the "username" property', () => {
expect(
chat().parse({
id: 882,
type: 'group chat',
title: 'My chat',
username: 'Johny Bravo',
}),
).toMatchObject({
username: 'Johny Bravo',
});
});
});
});
});
152 changes: 152 additions & 0 deletions packages/init-data/tests/initData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { describe, expect, it } from 'vitest';

import { initData } from '../src/index.js';

function createSearchParams(json: Record<string, unknown>): string {
const params = new URLSearchParams();

Object.entries(json).forEach(([key, value]) => {
params.set(
key,
typeof value === 'object' ? JSON.stringify(value) : String(value),
);
});

return params.toString();
}

describe('initData.ts', () => {
describe('initData', () => {
describe('auth_date', () => {
it('should throw an error in case, this property is missing', () => {
expect(() => initData().parse(createSearchParams({ hash: 'abcd' }))).toThrow();
});

it('should parse source property as Date and pass it to the "authDate" property', () => {
expect(initData().parse(createSearchParams({ auth_date: 1, hash: 'abcd' }))).toMatchObject({
authDate: new Date(1000),
});
});
});

describe('can_send_after', () => {
it('should parse source property as Date and pass it to the "canSendAfter" property', () => {
expect(
initData().parse(createSearchParams({
auth_date: 1,
hash: 'abcd',
can_send_after: 8882,
})),
).toMatchObject({
canSendAfter: 8882,
});
});
});

describe('chat', () => {
it('should parse source property as Chat and pass it to the "chat" property', () => {
expect(
initData().parse(createSearchParams({
auth_date: 1,
hash: 'abcd',
chat: {
id: 5,
type: 'group chat',
title: 'My Chat',
photo_url: 'https://johny.com',
username: 'Johny Chat',
},
})),
).toMatchObject({
chat: {
id: 5,
type: 'group chat',
title: 'My Chat',
photoUrl: 'https://johny.com',
username: 'Johny Chat',
},
});
});
});

describe('hash', () => {
it('should throw an error in case, this property is missing', () => {
expect(
() => initData().parse(createSearchParams({
auth_date: 1,
})),
).toThrow();
});

it('should parse source property as string and pass it to the "hash" property', () => {
expect(
initData().parse(createSearchParams({
auth_date: 1,
hash: 'abcd',
})),
).toMatchObject({
hash: 'abcd',
});
});
});

[
['chat_instance', 'chatInstance'],
['chat_type', 'chatType'],
['query_id', 'queryId'],
['start_param', 'startParam'],
].forEach(([from, to]) => {
describe(from, () => {
it(`should parse source property as string and pass it to the "${to}" property`, () => {
expect(
initData().parse(createSearchParams({
auth_date: 1,
hash: 'abcd',
[from]: 'my custom property',
})),
).toMatchObject({
[to]: 'my custom property',
});
});
});
});

['user', 'receiver'].forEach((property) => {
describe(property, () => {
it('should parse source property as User and pass it to the property with the same name', () => {
expect(
initData().parse(createSearchParams({
auth_date: 1,
hash: 'abcd',
[property]: {
added_to_attachment_menu: true,
allows_write_to_pm: false,
first_name: 'Johny',
id: 333,
is_bot: false,
is_premium: true,
language_code: 'en',
last_name: 'Bravo',
photo_url: 'https://johny.com',
username: 'johnybravo',
},
})),
).toMatchObject({
[property]: {
addedToAttachmentMenu: true,
allowsWriteToPm: false,
firstName: 'Johny',
id: 333,
isBot: false,
isPremium: true,
languageCode: 'en',
lastName: 'Bravo',
photoUrl: 'https://johny.com',
username: 'johnybravo',
},
});
});
});
});
});
});
Loading

1 comment on commit e3416e3

@vercel
Copy link

@vercel vercel bot commented on e3416e3 Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.