Skip to content

Commit

Permalink
Improve tests for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangomz committed Jun 22, 2021
1 parent 3e034c8 commit 5ac3a17
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 24 deletions.
41 changes: 21 additions & 20 deletions lib/notion/general/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Property {
static Property propertyFromJson(Map<String, dynamic> json) {
PropertiesTypes type = extractPropertyType(json);
if (type == PropertiesTypes.Title) {
bool contentIsList = TitleProp.contentIsList(json);
bool contentIsList = Property.contentIsList(json, type);
return TitleProp.fromJson(json, subfield: contentIsList ? null : 'title');
} else if (type == PropertiesTypes.RichText) {
return RichTextProp.fromJson(json);
Expand All @@ -76,6 +76,18 @@ class Property {
return Property();
}
}

/// Check if the specific json have a content list.
static bool contentIsList(Map<String, dynamic> json, PropertiesTypes type) =>
fieldIsList(json[propertyTypeToString(type)]);

/// Returns true if the properties are empty.
static bool isEmpty(Map<String, dynamic> json, PropertiesTypes type) {
if (json[propertyTypeToString(type)] != null) {
return json[propertyTypeToString(type)]!.isEmpty;
}
return true;
}
}

/// A representation of a title property for any Notion object.
Expand All @@ -94,26 +106,15 @@ class TitleProp extends Property {
/// Create a new property instance from json.
///
/// Receive a [json] from where the information is extracted.
TitleProp.fromJson(Map<String, dynamic> json,
{String? subfield, bool isEmpty: false})
: this.content = Text.fromListJson(isEmpty
? []
: ((subfield != null
? json[propertyTypeToString(PropertiesTypes.Title)]
[subfield]
: json[propertyTypeToString(PropertiesTypes.Title)]) ??
[]) as List)
TitleProp.fromJson(Map<String, dynamic> json, {String? subfield})
: this.content = Text.fromListJson(((subfield != null
? json[propertyTypeToString(PropertiesTypes.Title)]
[subfield]
: json[propertyTypeToString(PropertiesTypes.Title)]) ??
[]) as List)
.toList(),
super(id: json['id']);

/// Check if the specific json have a content list.
static bool contentIsList(Map<String, dynamic> json) =>
fieldIsList(json[propertyTypeToString(PropertiesTypes.Title)]);

/// Returns true if the properties are empty.
static bool isEmpty(Map<String, dynamic> json) =>
json[PropertiesTypes.Title]?.isEmpty;

/// The value of the content.
@override
List<Text> get value => this.content;
Expand Down Expand Up @@ -195,9 +196,9 @@ class MultiSelectProp extends Property {
super(id: json['id']);

/// Add a new [option] to the multi select options and returns this instance.
List<MultiSelectOption> addOption(MultiSelectOption option) {
MultiSelectProp addOption(MultiSelectOption option) {
this.options.add(option);
return this.options;
return this;
}

/// Convert this to a valid json representation for the Notion API.
Expand Down
93 changes: 89 additions & 4 deletions test/property_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ void main() {
test('Create an instance of property', () {
Property prop = Property.empty();
expect(prop.type, PropertiesTypes.None);
expect(prop.value, false);
});

test('Create a json from empty property', () {
Expand All @@ -32,14 +33,30 @@ void main() {
"type": "multi_select",
"multi_select": {"options": []}
},
"Name": {"id": "title", "type": "title", "title": {}}
"Name": {
"id": "title",
"type": "title",
"title": {},
},
"Details": {
'id': 'D[X|',
'type': 'rich_text',
"rich_text": [
{
"type": "text",
"text": {"content": "foo bar"}
}
]
}
});

expect(json, isNotEmpty);
expect(json, contains('Tags'));
expect(json['Tags']!.isMultiSelect, true);
expect(json, contains('Name'));
expect(json['Name']!.isTitle, true);
expect(json, contains('Details'));
expect(json['Details']!.isRichText, true);
});

test('Create json from Property inherited class', () {
Expand All @@ -51,6 +68,33 @@ void main() {
expect(json, contains(strType));
expect((json[strType] as List).length, 1);
});

test('Check if property is empty', () {
bool isEmptyTrue = Property.isEmpty(
{'id': 'title', 'type': 'title', 'title': []}, PropertiesTypes.Title);
bool isEmptyFalse = Property.isEmpty({
'id': 'title',
'type': 'title',
'title': [
{
"type": "text",
"text": {"content": "Page from Test", "link": null},
"annotations": {
"bold": false,
"italic": false,
"strikethrough": false,
"underline": false,
"code": false,
"color": "default"
},
"plain_text": "Page from Test",
"href": null
}
]
}, PropertiesTypes.Title);
expect(isEmptyTrue, true);
expect(isEmptyFalse, false);
});
});

group('Title property =>', () {
Expand All @@ -61,6 +105,7 @@ void main() {
expect(prop.content, isNotEmpty);
expect(prop.content.length, 1);
});

test('Create a json from property', () {
Map<String, dynamic> json = TitleProp(content: [Text('TITLE')]).toJson();

Expand All @@ -78,6 +123,8 @@ void main() {
expect(rich.type, PropertiesTypes.RichText);
expect(rich.content, isNotEmpty);
expect(rich.content.length, 2);
expect(rich.value, isNotEmpty);
expect(rich.value.length, 2);
});
test('Create a json from property', () {
Map<String, dynamic> json =
Expand All @@ -100,6 +147,18 @@ void main() {
expect(multi.options.length, 1);
});

test('Create an instance with mixed options', () {
MultiSelectProp multi =
MultiSelectProp(options: [MultiSelectOption(name: 'A')])
.addOption(MultiSelectOption(name: 'B'))
.addOption(MultiSelectOption(name: 'C'));

expect(multi.type, PropertiesTypes.MultiSelect);
expect(multi.options, hasLength(3));
expect(multi.options.first.name, 'A');
expect(multi.options.last.name, 'C');
});

test('Create an option for multi select', () {
MultiSelectOption option = MultiSelectOption(name: 'A');

Expand Down Expand Up @@ -129,7 +188,7 @@ void main() {
expect(json[strType]['options'], isNotEmpty);
});

test('Create a json from option', () {
test('Create a json from option without id', () {
Map<String, dynamic> json =
MultiSelectOption(name: 'A', color: ColorsTypes.Brown).toJson();

Expand All @@ -138,10 +197,20 @@ void main() {
expect(json['color'], contains(colorTypeToString(ColorsTypes.Brown)));
});

test('Create a json from option with id', () {
Map<String, dynamic> json =
MultiSelectOption(name: 'A', color: ColorsTypes.Brown, id: 'a')
.toJson();

expect(json['name'], 'A');
expect(json['id'], isNotNull);
expect(json['color'], contains(colorTypeToString(ColorsTypes.Brown)));
});

test('Create an options list from json', () {
List<MultiSelectOption> list = MultiSelectOption.fromListJson([
{'name': 'A'},
{'name': 'B'}
{'name': 'A', 'id': 'a'},
{'name': 'B', 'id': 'b'},
]);

expect(list, isNotEmpty);
Expand Down Expand Up @@ -172,6 +241,8 @@ void main() {
};

Map<String, dynamic> jsonDetails = {
'id': 'D[X|',
'type': 'rich_text',
"rich_text": [
{
"type": "text",
Expand Down Expand Up @@ -239,6 +310,7 @@ void main() {

String strType = propertyTypeToString(PropertiesTypes.RichText);
expect(jsonTest['type'], strType);
expect(jsonTest['id'], isNotNull);
expect(jsonTest, contains(strType));
expect(jsonTest[strType], isList);
});
Expand All @@ -257,5 +329,18 @@ void main() {

expect(multi.options, isNotEmpty);
});

test('Create json from tags json response', () {
Map<String, dynamic> multi = MultiSelectProp.fromJson(
jsonMultiSelectWithSubfield,
subfield: 'options')
.toJson();

String strType = propertyTypeToString(PropertiesTypes.MultiSelect);
expect(multi['type'], strType);
expect(multi['id'], isNotNull);
expect(multi, contains(strType));
expect(multi[strType], isMap);
});
});
}

0 comments on commit 5ac3a17

Please sign in to comment.