-
Notifications
You must be signed in to change notification settings - Fork 2
π§ͺ [testing improvement] Add tests for getRecentAppIds error paths #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sunnylqm
wants to merge
2
commits into
main
Choose a base branch
from
jules-2911372941532518971-4a753c6d
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,73 @@ | ||
| cat << 'INNER_EOF' > src/globals.d.ts | ||
| declare module '*.svg' { | ||
| import type { FunctionComponent, SVGProps } from 'react'; | ||
|
|
||
| const content: string; | ||
| export default content; | ||
| export const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement>>; | ||
| } | ||
|
|
||
| declare module '*.png' { | ||
| const content: string; | ||
| export default content; | ||
| } | ||
|
|
||
| declare module '*.jpg' { | ||
| const content: string; | ||
| export default content; | ||
| } | ||
|
|
||
| declare module '*.css' { | ||
| const content: Record<string, string>; | ||
| export default content; | ||
| } | ||
|
|
||
| declare module 'bun:test' { | ||
| type TestHandler = () => void | Promise<void>; | ||
|
|
||
| export function describe(name: string, fn: TestHandler): void; | ||
| export function it(name: string, fn: TestHandler): void; | ||
| export function test(name: string, fn: TestHandler): void; | ||
| export function expect<T>(actual: T): { | ||
| toBe(expected: unknown): void; | ||
| toBeNull(): void; | ||
| toEqual(expected: unknown): void; | ||
| toContain(expected: unknown): void; | ||
| toHaveBeenCalledWith(...args: unknown[]): void; | ||
| toHaveBeenCalled(): void; | ||
| not: { | ||
| toHaveBeenCalledWith(...args: unknown[]): void; | ||
| toHaveBeenCalled(): void; | ||
| }; | ||
| }; | ||
| export function beforeEach(fn: () => void | Promise<void>): void; | ||
| export function afterEach(fn: () => void | Promise<void>): void; | ||
| export function setSystemTime(time: Date | number | null): void; | ||
| export const mock: { | ||
| module(path: string, factory: () => any): void; | ||
| <T extends (...args: any[]) => any>( | ||
| fn?: T, | ||
| ): T & { mockClear(): void; mockImplementationOnce(fn: T): void }; | ||
| }; | ||
| } | ||
|
|
||
| type Tier = import('./types').Tier; | ||
|
|
||
| type User = import('./types').User; | ||
| type AdminUser = import('./types').AdminUser; | ||
| type AdminApp = import('./types').AdminApp; | ||
| type AdminVersion = import('./types').AdminVersion; | ||
| type Quota = import('./types').Quota; | ||
| type App = import('./types').App; | ||
| type PackageBase = import('./types').PackageBase; | ||
| type Package = import('./types').Package; | ||
| type Commit = import('./types').Commit; | ||
| type Version = import('./types').Version; | ||
| type AppDetail = import('./types').AppDetail; | ||
| type ContentProps = import('./types').ContentProps; | ||
| type VersionConfig = import('./types').VersionConfig; | ||
| type BindingType = import('./types').BindingType; | ||
| type Binding = import('./types').Binding; | ||
| type AuditLog = import('./types').AuditLog; | ||
| type ApiToken = import('./types').ApiToken; | ||
| INNER_EOF |
This file contains hidden or 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,137 @@ | ||
| cat << 'INNER_EOF' > src/utils/helper.test.ts | ||
| import { afterEach, describe, expect, test } from 'bun:test'; | ||
| import { | ||
| getRecentAppIds, | ||
| isExpVersion, | ||
| isPasswordValid, | ||
| isValidExternalUrl, | ||
| RECENT_APP_STORAGE_KEY, | ||
| } from './helper'; | ||
|
|
||
| describe('isPasswordValid', () => { | ||
| test('should return true for valid passwords', () => { | ||
| expect(isPasswordValid('Passw0rd')).toBe(true); | ||
| expect(isPasswordValid('UPPER123')).toBe(true); | ||
| expect(isPasswordValid('Valid123')).toBe(true); | ||
| }); | ||
|
|
||
| test('should return false for passwords that are too short', () => { | ||
| expect(isPasswordValid('Short')).toBe(false); // 5 chars | ||
| expect(isPasswordValid('A1b')).toBe(false); // 3 chars | ||
| }); | ||
|
|
||
| test('should return false for passwords that are too long', () => { | ||
| expect(isPasswordValid('ThisPasswordIsWayTooLong123')).toBe(false); // > 16 chars | ||
| }); | ||
|
|
||
| test('should return false for passwords with only digits', () => { | ||
| expect(isPasswordValid('12345678')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false for passwords with only lowercase letters', () => { | ||
| expect(isPasswordValid('lowercase')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false for passwords with no uppercase letters', () => { | ||
| expect(isPasswordValid('lower123')).toBe(false); | ||
| expect(isPasswordValid('noupper!')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isExpVersion', () => { | ||
| test('should return false when config is null', () => { | ||
| expect(isExpVersion(null, '1.0.0')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false when config is undefined', () => { | ||
| expect(isExpVersion(undefined, '1.0.0')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false when config.rollout is missing', () => { | ||
| expect(isExpVersion({}, '1.0.0')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false when rollout config for version is missing', () => { | ||
| expect(isExpVersion({ rollout: {} }, '1.0.0')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false when rollout config for version is null', () => { | ||
| expect(isExpVersion({ rollout: { '1.0.0': null } }, '1.0.0')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return true when rollout is less than 100', () => { | ||
| expect(isExpVersion({ rollout: { '1.0.0': 50 } }, '1.0.0')).toBe(true); | ||
| expect(isExpVersion({ rollout: { '1.0.0': 0 } }, '1.0.0')).toBe(true); | ||
| }); | ||
|
|
||
| test('should return false when rollout is 100', () => { | ||
| expect(isExpVersion({ rollout: { '1.0.0': 100 } }, '1.0.0')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false when rollout is greater than 100', () => { | ||
| expect(isExpVersion({ rollout: { '1.0.0': 110 } }, '1.0.0')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getRecentAppIds', () => { | ||
| afterEach(() => { | ||
| window.localStorage.clear(); | ||
| }); | ||
|
|
||
| test('should return empty array when localStorage contains invalid JSON', () => { | ||
| window.localStorage.setItem(RECENT_APP_STORAGE_KEY, 'invalid json'); | ||
| expect(getRecentAppIds()).toEqual([]); | ||
| }); | ||
|
|
||
| test('should return empty array when localStorage contains non-array JSON', () => { | ||
| window.localStorage.setItem(RECENT_APP_STORAGE_KEY, '{"a": 1}'); | ||
| expect(getRecentAppIds()).toEqual([]); | ||
| }); | ||
|
|
||
| test('should return filtered array of integers when localStorage contains valid array', () => { | ||
| window.localStorage.setItem(RECENT_APP_STORAGE_KEY, '[1, "2", 3.5, 4]'); | ||
| expect(getRecentAppIds()).toEqual([1, 4]); | ||
| }); | ||
|
|
||
| test('should return empty array when window is undefined', () => { | ||
| const originalWindow = global.window; | ||
| // @ts-expect-error | ||
| delete global.window; | ||
| expect(getRecentAppIds()).toEqual([]); | ||
| global.window = originalWindow; | ||
| }); | ||
| }); | ||
|
|
||
| describe('isValidExternalUrl', () => { | ||
| test('should return true for valid https URLs with trusted domains', () => { | ||
| expect(isValidExternalUrl('https://react-native.cn/path')).toBe(true); | ||
| expect(isValidExternalUrl('https://sub.react-native.cn/path')).toBe(true); | ||
| expect(isValidExternalUrl('https://reactnative.cn/')).toBe(true); | ||
| expect(isValidExternalUrl('https://rnupdate.online/foo')).toBe(true); | ||
| expect(isValidExternalUrl('https://alipay.com/pay')).toBe(true); | ||
| expect(isValidExternalUrl('https://openapi.alipay.com/gateway.do')).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| test('should return false for http protocol', () => { | ||
| expect(isValidExternalUrl('http://react-native.cn/path')).toBe(false); | ||
| expect(isValidExternalUrl('http://alipay.com')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false for untrusted domains', () => { | ||
| expect(isValidExternalUrl('https://evil.com/path')).toBe(false); | ||
| expect(isValidExternalUrl('https://google.com')).toBe(false); | ||
| expect(isValidExternalUrl('https://react-native.cnevil.com')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false for malformed URLs', () => { | ||
| expect(isValidExternalUrl('not a url')).toBe(false); | ||
| expect(isValidExternalUrl('://bad-url')).toBe(false); | ||
| }); | ||
|
|
||
| test('should return false for javascript uris', () => { | ||
| expect(isValidExternalUrl('javascript:alert(1)')).toBe(false); | ||
| }); | ||
| }); | ||
| INNER_EOF |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Reset button loading state when payment URL is invalid/missing.
This branch can return without redirect, but callers keep
loading=true, so purchase buttons can remain stuck after a rejected URL.Proposed fix
π€ Prompt for AI Agents