diff --git a/app/Devnull/PostAs.php b/app/Devnull/PostAs.php index d57f5fb7..26711721 100644 --- a/app/Devnull/PostAs.php +++ b/app/Devnull/PostAs.php @@ -60,6 +60,9 @@ public static function offered(?string $picked): bool * an organization, as elsewhere in the app, and the one posting now ticked. Until an avatar loads, or if it * can't, a person or building icon stands in. $method is called with the handle picked, or '' for you. * + * Each row is the name with the handle under it, smaller, which screen readers read after the name. A core + * without NavAction::subtitle() (before NativePHP/mobile-air#740) gets one label, "Simon (@simon-hamp)", instead. + * * @return list */ public static function menu(User $me, ?User $author, string $method): array @@ -67,18 +70,34 @@ public static function menu(User $me, ?User $author, string $method): array return array_map(function (User $identity) use ($author, $method): TopBarAction { $icon = $identity->isOrganization ? Lucide::Building2 : Lucide::UserRound; - return NavAction::make('post-as-'.$identity->username) - ->label($identity->name.' (@'.$identity->username.')') + $handle = '@'.$identity->username; + $subtitled = self::subtitles(); + + $row = NavAction::make('post-as-'.$identity->username) + ->label($subtitled ? $identity->name : $identity->name.' ('.$handle.')') ->icon(ios: $icon->ios(), android: $icon->android()) ->image(ImageCache::src($identity->avatarUrl)) - ->press($method."('".($identity->isOrganization ? $identity->username : '')."')") - ->toElement() + ->press($method."('".($identity->isOrganization ? $identity->username : '')."')"); + + if ($subtitled) { + $row->subtitle($handle); + } + + return $row->toElement() ->setProp('image_shape', $identity->isOrganization ? 'rounded' : 'circle') ->setProp('checked', self::posting($identity, $author)) ->ref('post-as-'.$identity->username); }, [$me, ...self::organizations()]); } + /** + * Whether the installed core can give a menu row a subtitle under its label. + */ + private static function subtitles(): bool + { + return method_exists(NavAction::class, 'subtitle'); + } + /** * What the composer's avatar says to a screen reader. */ diff --git a/tests/Feature/Screens/PostAsTest.php b/tests/Feature/Screens/PostAsTest.php index 21550679..700b2202 100644 --- a/tests/Feature/Screens/PostAsTest.php +++ b/tests/Feature/Screens/PostAsTest.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Http; use Native\Mobile\Testing\Native; use Native\Mobile\Testing\TestableComponent; +use Tests\Support\MenuSubtitle; beforeEach(function () { config(['devnull.url' => 'http://devnull.test']); @@ -68,6 +69,18 @@ function composeOnLogAs(?array $answer = null, array $extra = []): TestableCompo return Native::visit('/log', platform: 'ios')->press('composeInSheet'); } +/** + * The Post as menu a pressable opens, as each row's ref, label and subtitle. + * + * @return list + */ +function postAsRows(TestableComponent $screen, string $ref): array +{ + $rows = array_filter(nodeByRef($screen, $ref)['children'] ?? [], fn (array $child): bool => ($child['type'] ?? null) === 'top_bar_action'); + + return array_values(array_map(fn (array $row): array => [$row['ref'] ?? null, $row['props']['label'] ?? null, $row['props']['subtitle'] ?? null], $rows)); +} + /** * The JSON of the commits sent to devnull. * @@ -157,11 +170,7 @@ function commitsSent(): array expect(nodeByRef($screen, 'sheet-post-as-name')['props']['text'])->toBe('Local Dev') ->and(nodeByRef($screen, 'sheet-post-as-handle')['props']['text'])->toBe('@local_dev') ->and(nodeByRef($screen, 'sheet-post-as')['props']['a11y_label'])->toBe('Posting as @local_dev. Choose who to post as') - ->and(array_map(fn (array $item): array => [$item[0], $item[1]], menuItems($screen, 'sheet-post-as')))->toBe([ - ['post-as-local_dev', 'Local Dev (@local_dev)'], - ['post-as-dev-null-sh', '/dev/null (@dev-null-sh)'], - ['post-as-nativephp', 'NativePHP (@nativephp)'], - ]); + ->and(array_column(menuItems($screen, 'sheet-post-as'), 0))->toBe(['post-as-local_dev', 'post-as-dev-null-sh', 'post-as-nativephp']); $screen->call('postAsInSheet', 'nativephp'); @@ -178,6 +187,27 @@ function commitsSent(): array expect(sheetButtonCircle($screen, 'sheet-commit')['style']['bg_color'])->not->toBe($organizationFill); }); +it('names each account in the Post as menu, with its handle under the name', function () { + MenuSubtitle::skipUnlessSupported(); + memberOf([memberOrganizationJson(), memberOrganizationJson(['id' => 'o2', 'handle' => 'nativephp', 'name' => 'NativePHP', 'role' => 'member'])]); + + expect(postAsRows(composeOnLogAs(), 'sheet-post-as'))->toBe([ + ['post-as-local_dev', 'Local Dev', '@local_dev'], + ['post-as-dev-null-sh', '/dev/null', '@dev-null-sh'], + ['post-as-nativephp', 'NativePHP', '@nativephp'], + ]); +}); + +it('puts the handle in the label when core has no menu row subtitles', function () { + MenuSubtitle::skipIfSupported(); + memberOf([memberOrganizationJson()]); + + expect(postAsRows(composeOnLogAs(), 'sheet-post-as'))->toBe([ + ['post-as-local_dev', 'Local Dev (@local_dev)', null], + ['post-as-dev-null-sh', '/dev/null (@dev-null-sh)', null], + ]); +}); + it('commits as the organization picked, and shows it staged as the organization', function () { memberOf([memberOrganizationJson()]); holdRefreshes(); diff --git a/tests/Support/MenuSubtitle.php b/tests/Support/MenuSubtitle.php new file mode 100644 index 00000000..4ad7d904 --- /dev/null +++ b/tests/Support/MenuSubtitle.php @@ -0,0 +1,32 @@ +markTestSkipped('Needs a core with NavAction::subtitle() (NativePHP/mobile-air#740)'); + } + } + + public static function skipIfSupported(): void + { + if (self::supported()) { + test()->markTestSkipped('The installed core has NavAction::subtitle(), so the menu never falls back to one label'); + } + } +}