-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from Telegram-Mini-Apps/157-add-missing-init-…
…data-parameters 157 add missing init data parameters
- Loading branch information
Showing
6 changed files
with
478 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
e3416e3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./apps/docs
docs-git-master-telegram-mini-apps.vercel.app
tma-js-docs.vercel.app
docs.telegram-mini-apps.com
docs-telegram-mini-apps.vercel.app