Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions app/Devnull/PostAs.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,44 @@ 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<TopBarAction>
*/
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.
*/
Expand Down
40 changes: 35 additions & 5 deletions tests/Feature/Screens/PostAsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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<array{0: string|null, 1: string|null, 2: string|null}>
*/
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.
*
Expand Down Expand Up @@ -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');

Expand All @@ -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();
Expand Down
32 changes: 32 additions & 0 deletions tests/Support/MenuSubtitle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Support;

use Native\Mobile\Edge\Layouts\Builders\NavAction;

/**
* Whether the installed core can give a menu row a subtitle (`NavAction::subtitle()`, from NativePHP/mobile-air#740;
* mobile-ui draws it since NativePHP/mobile-ui#228). Without it the Post as menu puts the handle in the label,
* "Simon (@simon-hamp)", so the tests of the subtitle skip.
*/
final class MenuSubtitle
{
public static function supported(): bool
{
return method_exists(NavAction::class, 'subtitle');
}

public static function skipUnlessSupported(): void
{
if (! self::supported()) {
test()->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');
}
}
}
Loading