|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:codelessly_api/codelessly_api.dart'; |
| 4 | +import 'package:test/test.dart'; |
| 5 | + |
| 6 | +VarianceNode responsive(List<Variant> variants, {String? current}) => |
| 7 | + VarianceNode( |
| 8 | + id: 'layout', |
| 9 | + name: 'Responsive layout', |
| 10 | + basicBoxLocal: NodeBox(0, 0, 390, 844), |
| 11 | + variants: variants, |
| 12 | + currentVariantId: current, |
| 13 | + ); |
| 14 | + |
| 15 | +Variant variant(String id, [ScreenBreakpoint? breakpoint]) => |
| 16 | + Variant(id: id, name: id, children: ['$id-root'], breakpoint: breakpoint); |
| 17 | + |
| 18 | +void main() { |
| 19 | + group('ScreenBreakpoint.forWidth', () { |
| 20 | + test('follows the screen variable rule (600 and 1024)', () { |
| 21 | + expect(ScreenBreakpoint.forWidth(0), ScreenBreakpoint.mobile); |
| 22 | + expect(ScreenBreakpoint.forWidth(390), ScreenBreakpoint.mobile); |
| 23 | + expect(ScreenBreakpoint.forWidth(599.9), ScreenBreakpoint.mobile); |
| 24 | + expect(ScreenBreakpoint.forWidth(600), ScreenBreakpoint.tablet); |
| 25 | + expect(ScreenBreakpoint.forWidth(834), ScreenBreakpoint.tablet); |
| 26 | + expect(ScreenBreakpoint.forWidth(1023.9), ScreenBreakpoint.tablet); |
| 27 | + expect(ScreenBreakpoint.forWidth(1024), ScreenBreakpoint.desktop); |
| 28 | + expect(ScreenBreakpoint.forWidth(1440), ScreenBreakpoint.desktop); |
| 29 | + }); |
| 30 | + |
| 31 | + test('default widths fall in their own breakpoint', () { |
| 32 | + for (final ScreenBreakpoint breakpoint in ScreenBreakpoint.values) { |
| 33 | + expect(ScreenBreakpoint.forWidth(breakpoint.defaultWidth), breakpoint); |
| 34 | + } |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + group('Variant.breakpoint JSON', () { |
| 39 | + test('round-trips and is omitted when unset', () { |
| 40 | + final Variant plain = variant('plain'); |
| 41 | + expect(plain.toJson().containsKey('breakpoint'), isFalse); |
| 42 | + expect(Variant.fromJson(plain.toJson()), plain); |
| 43 | + |
| 44 | + final Variant tablet = variant('tablet', ScreenBreakpoint.tablet); |
| 45 | + expect(tablet.toJson()['breakpoint'], 'tablet'); |
| 46 | + expect(Variant.fromJson(tablet.toJson()), tablet); |
| 47 | + expect( |
| 48 | + Variant.fromJson(tablet.toJson()).breakpoint, |
| 49 | + ScreenBreakpoint.tablet, |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + test('a variance from before breakpoints reads as not responsive', () { |
| 54 | + // JSON written before breakpoints existed: no `breakpoint` keys. |
| 55 | + final Map json = jsonDecode( |
| 56 | + jsonEncode(responsive([variant('default')]).toJson()), |
| 57 | + ) as Map; |
| 58 | + expect(jsonEncode(json).contains('breakpoint'), isFalse); |
| 59 | + final VarianceNode node = VarianceNode.fromJson(json); |
| 60 | + expect(node.isResponsive, isFalse); |
| 61 | + expect(node.variants.single.breakpoint, isNull); |
| 62 | + expect(node.toJson()['variants'][0].containsKey('breakpoint'), isFalse); |
| 63 | + }); |
| 64 | + |
| 65 | + test('a responsive variance survives a JSON-encoded round trip', () { |
| 66 | + final VarianceNode node = responsive([ |
| 67 | + variant('m', ScreenBreakpoint.mobile), |
| 68 | + variant('t', ScreenBreakpoint.tablet), |
| 69 | + variant('d', ScreenBreakpoint.desktop), |
| 70 | + ], current: 't'); |
| 71 | + final Map decoded = jsonDecode(jsonEncode(node.toJson())) as Map; |
| 72 | + final VarianceNode back = VarianceNode.fromJson(decoded); |
| 73 | + expect(back.isResponsive, isTrue); |
| 74 | + expect(back.variants.map((Variant v) => v.breakpoint).toList(), [ |
| 75 | + ScreenBreakpoint.mobile, |
| 76 | + ScreenBreakpoint.tablet, |
| 77 | + ScreenBreakpoint.desktop, |
| 78 | + ]); |
| 79 | + expect(back.currentVariantId, 't'); |
| 80 | + expect(back.variants, node.variants); |
| 81 | + // Read through the registry, as a page document is loaded. |
| 82 | + final BaseNode viaRegistry = NodeJsonConverter().fromJson(decoded)!; |
| 83 | + expect((viaRegistry as VarianceNode).variantForWidth(1440).id, 'd'); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + group('VarianceNode.variantForWidth', () { |
| 88 | + test('picks the exact breakpoint', () { |
| 89 | + final VarianceNode node = responsive([ |
| 90 | + variant('m', ScreenBreakpoint.mobile), |
| 91 | + variant('t', ScreenBreakpoint.tablet), |
| 92 | + variant('d', ScreenBreakpoint.desktop), |
| 93 | + ]); |
| 94 | + expect(node.variantForWidth(390).id, 'm'); |
| 95 | + expect(node.variantForWidth(834).id, 't'); |
| 96 | + expect(node.variantForWidth(1440).id, 'd'); |
| 97 | + expect(node.children, ['m-root']); |
| 98 | + }); |
| 99 | + |
| 100 | + test('falls back to the nearest, the smaller one on a tie', () { |
| 101 | + final VarianceNode mobileDesktop = responsive([ |
| 102 | + variant('m', ScreenBreakpoint.mobile), |
| 103 | + variant('d', ScreenBreakpoint.desktop), |
| 104 | + ]); |
| 105 | + expect(mobileDesktop.variantForWidth(834).id, 'm'); |
| 106 | + expect(mobileDesktop.variantForWidth(1440).id, 'd'); |
| 107 | + |
| 108 | + final VarianceNode tabletOnly = responsive([ |
| 109 | + variant('plain'), |
| 110 | + variant('t', ScreenBreakpoint.tablet), |
| 111 | + ]); |
| 112 | + expect(tabletOnly.variantForWidth(390).id, 't'); |
| 113 | + expect(tabletOnly.variantForWidth(1440).id, 't'); |
| 114 | + |
| 115 | + final VarianceNode mobileTablet = responsive([ |
| 116 | + variant('m', ScreenBreakpoint.mobile), |
| 117 | + variant('t', ScreenBreakpoint.tablet), |
| 118 | + ]); |
| 119 | + expect(mobileTablet.variantForWidth(1440).id, 't'); |
| 120 | + }); |
| 121 | + |
| 122 | + test('an ordinary variance keeps its current variant', () { |
| 123 | + final VarianceNode node = responsive([ |
| 124 | + variant('a'), |
| 125 | + variant('b'), |
| 126 | + ], current: 'b'); |
| 127 | + expect(node.isResponsive, isFalse); |
| 128 | + expect(node.variantForBreakpoint(ScreenBreakpoint.mobile), isNull); |
| 129 | + expect(node.variantForWidth(390).id, 'b'); |
| 130 | + }); |
| 131 | + }); |
| 132 | +} |
0 commit comments