Skip to content

Commit

Permalink
Add screen.debug
Browse files Browse the repository at this point in the history
  • Loading branch information
sydneyjodon-wk committed Jun 16, 2021
1 parent 3777907 commit f636248
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 113 deletions.
3 changes: 1 addition & 2 deletions lib/dom/queries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ library rtl.dom.queries;
export '../src/dom/async/types.dart' show MutationObserverOptions;
export '../src/dom/matches/get_default_normalizer.dart' show getDefaultNormalizer;
export '../src/dom/matches/types.dart' show NormalizerFn, NormalizerOptions;
export '../src/dom/screen.dart' show screen;
export '../src/dom/within.dart' show within, WithinQueries;
export '../src/dom/within.dart' show within, WithinQueries, screen, ScreenQueries;
// ignore: directives_ordering
export '../src/dom/top_level_queries.dart'
show
Expand Down
28 changes: 0 additions & 28 deletions lib/src/dom/screen.dart

This file was deleted.

31 changes: 31 additions & 0 deletions lib/src/dom/within.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

@JS()
library react_testing_library.src.dom.within;

import 'dart:html' show Node, ShadowRoot, document;

import 'package:js/js.dart';
import 'package:meta/meta.dart';
import 'package:react_testing_library/src/dom/pretty_dom.dart';
import 'package:react_testing_library/src/dom/scoped_queries.dart' show ScopedQueries;
import 'package:react_testing_library/src/util/is_or_contains.dart';

Expand Down Expand Up @@ -59,3 +64,29 @@ WithinQueries within(Node node) {

return WithinQueries._(node);
}

@sealed
class ScreenQueries extends WithinQueries {
ScreenQueries._() : super._(document.body);

/// A shortcut for `console.log(prettyDOM(document.body))`.
///
/// > See: <https://testing-library.com/docs/queries/about/#screendebug>
void debug([
Node baseElement,
int maxLength,
PrettyDomOptions options,
]) =>
_screen.debug(baseElement, maxLength, options);
}

/// Exposes all the "top-level" queries exposed by the dom-testing-library,
/// but the scope/container is defaulted to `document.body`.
///
/// > See: <https://testing-library.com/docs/queries/about/#screen>
///
/// {@category Queries}
final screen = ScreenQueries._();

@JS('rtl.screen')
external dynamic get _screen;
3 changes: 2 additions & 1 deletion lib/src/react/render/render.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:meta/meta.dart';
import 'package:react/react_client.dart' show ReactComponentFactoryProxy, ReactElement;
import 'package:react_testing_library/src/dom/pretty_dom.dart';
import 'package:react_testing_library/src/dom/scoped_queries.dart' show ScopedQueries;
import 'package:react_testing_library/src/dom/within.dart' show ScreenQueries;
import 'package:test/test.dart' show addTearDown;

import 'package:react_testing_library/src/react/render/types.dart' show JsRenderResult, RenderOptions;
Expand Down Expand Up @@ -180,7 +181,7 @@ class RenderResult extends ScopedQueries {

/// A shortcut for `console.log(prettyDOM(baseElement))`.
///
/// > __NOTE: It's recommended to use `screen.debug` instead.__
/// > __NOTE: It's recommended to use [ScreenQueries.debug] instead.__
///
/// > See: <https://testing-library.com/docs/react-testing-library/api/#debug>
void debug([
Expand Down
82 changes: 0 additions & 82 deletions test/unit/dom/screen_test.dart

This file was deleted.

64 changes: 64 additions & 0 deletions test/unit/dom/within_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import 'dart:html';

import 'package:react/react_client.dart' show ReactElement;
import 'package:react_testing_library/dom/queries.dart' show WithinQueries;
import 'package:react_testing_library/react_testing_library.dart' as rtl;
import 'package:test/test.dart';

Expand Down Expand Up @@ -52,4 +53,67 @@ void main() {
expect(rtl.within(nodeWithShadowRoot.shadowRoot).getByRole('button'), isA<ButtonElement>());
});
});

group('screen', () {
test('aliases within(document.body)', () {
final queries = rtl.screen;
expect(queries, isA<WithinQueries>());
expect(queries.container, document.body);
});

test('exposes a debug method', () {
expect(rtl.screen.debug, isA<Function>());
});

test('exposes all the expected queries', () {
expect(rtl.screen.getByAltText, isA<Function>(), reason: 'getByAltText');
expect(rtl.screen.getAllByAltText, isA<Function>(), reason: 'getAllByAltText');
expect(rtl.screen.queryByAltText, isA<Function>(), reason: 'queryByAltText');
expect(rtl.screen.queryAllByAltText, isA<Function>(), reason: 'queryAllByAltText');
expect(rtl.screen.findByAltText, isA<Function>(), reason: 'findByAltText');
expect(rtl.screen.findAllByAltText, isA<Function>(), reason: 'findAllByAltText');
expect(rtl.screen.getByDisplayValue, isA<Function>(), reason: 'getByDisplayValue');
expect(rtl.screen.getAllByDisplayValue, isA<Function>(), reason: 'getAllByDisplayValue');
expect(rtl.screen.queryByDisplayValue, isA<Function>(), reason: 'queryByDisplayValue');
expect(rtl.screen.queryAllByDisplayValue, isA<Function>(), reason: 'queryAllByDisplayValue');
expect(rtl.screen.findByDisplayValue, isA<Function>(), reason: 'findByDisplayValue');
expect(rtl.screen.findAllByDisplayValue, isA<Function>(), reason: 'findAllByDisplayValue');
expect(rtl.screen.getByLabelText, isA<Function>(), reason: 'getByLabelText');
expect(rtl.screen.getAllByLabelText, isA<Function>(), reason: 'getAllByLabelText');
expect(rtl.screen.queryByLabelText, isA<Function>(), reason: 'queryByLabelText');
expect(rtl.screen.queryAllByLabelText, isA<Function>(), reason: 'queryAllByLabelText');
expect(rtl.screen.findByLabelText, isA<Function>(), reason: 'findByLabelText');
expect(rtl.screen.findAllByLabelText, isA<Function>(), reason: 'findAllByLabelText');
expect(rtl.screen.getByPlaceholderText, isA<Function>(), reason: 'getByPlaceholderText');
expect(rtl.screen.getAllByPlaceholderText, isA<Function>(), reason: 'getAllByPlaceholderText');
expect(rtl.screen.queryByPlaceholderText, isA<Function>(), reason: 'queryByPlaceholderText');
expect(rtl.screen.queryAllByPlaceholderText, isA<Function>(), reason: 'queryAllByPlaceholderText');
expect(rtl.screen.findByPlaceholderText, isA<Function>(), reason: 'findByPlaceholderText');
expect(rtl.screen.findAllByPlaceholderText, isA<Function>(), reason: 'findAllByPlaceholderText');
expect(rtl.screen.getByRole, isA<Function>(), reason: 'getByRole');
expect(rtl.screen.getAllByRole, isA<Function>(), reason: 'getAllByRole');
expect(rtl.screen.queryByRole, isA<Function>(), reason: 'queryByRole');
expect(rtl.screen.queryAllByRole, isA<Function>(), reason: 'queryAllByRole');
expect(rtl.screen.findByRole, isA<Function>(), reason: 'findByRole');
expect(rtl.screen.findAllByRole, isA<Function>(), reason: 'findAllByRole');
expect(rtl.screen.getByTestId, isA<Function>(), reason: 'getByTestId');
expect(rtl.screen.getAllByTestId, isA<Function>(), reason: 'getAllByTestId');
expect(rtl.screen.queryByTestId, isA<Function>(), reason: 'queryByTestId');
expect(rtl.screen.queryAllByTestId, isA<Function>(), reason: 'queryAllByTestId');
expect(rtl.screen.findByTestId, isA<Function>(), reason: 'findByTestId');
expect(rtl.screen.findAllByTestId, isA<Function>(), reason: 'findAllByTestId');
expect(rtl.screen.getByText, isA<Function>(), reason: 'getByText');
expect(rtl.screen.getAllByText, isA<Function>(), reason: 'getAllByText');
expect(rtl.screen.queryByText, isA<Function>(), reason: 'queryByText');
expect(rtl.screen.queryAllByText, isA<Function>(), reason: 'queryAllByText');
expect(rtl.screen.findByText, isA<Function>(), reason: 'findByText');
expect(rtl.screen.findAllByText, isA<Function>(), reason: 'findAllByText');
expect(rtl.screen.getByTitle, isA<Function>(), reason: 'getByTitle');
expect(rtl.screen.getAllByTitle, isA<Function>(), reason: 'getAllByTitle');
expect(rtl.screen.queryByTitle, isA<Function>(), reason: 'queryByTitle');
expect(rtl.screen.queryAllByTitle, isA<Function>(), reason: 'queryAllByTitle');
expect(rtl.screen.findByTitle, isA<Function>(), reason: 'findByTitle');
expect(rtl.screen.findAllByTitle, isA<Function>(), reason: 'findAllByTitle');
});
});
}

0 comments on commit f636248

Please sign in to comment.