Skip to content

Commit

Permalink
tests: additional data-test selectors which will make testing much ea…
Browse files Browse the repository at this point in the history
…sier in the future

fix: clear tag selection on category change but not document selection change
tests: add two tests covering the aforementioned tag selection issues
  • Loading branch information
StephanH90 authored and czosel committed Nov 25, 2021
1 parent b01c971 commit 48e8a0b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 19 deletions.
5 changes: 4 additions & 1 deletion addon/components/category-nav/category.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
{{set-style color=@category.color}}
/>
</div>
<div data-test-name>
<div
data-test-name
data-test-category-id={{@category.id}}
>
{{@category.name}}
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions addon/components/category-nav/category.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getOwner } from "@ember/application";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
import Component from "@glimmer/component";
Expand All @@ -6,13 +7,20 @@ export default class CategoryNavCategoryComponent extends Component {
@service documents;
@service router;

get controllerInstance() {
const applicationInstance = getOwner(this);
return applicationInstance.lookup("controller:application");
}

@action loadCategory() {
this.documents.clearDocumentSelection();
this.controllerInstance.resetTagFilter();
this.router.transitionTo({
queryParams: {
category: this.args.category.id,
search: undefined,
document: undefined,
tags: undefined,
},
});
}
Expand Down
1 change: 1 addition & 0 deletions addon/components/document-list-item.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<tr
class={{if @isSelected "document-list-item-selected" "document-list-item"}}
data-test-document-list-item
data-test-document-list-item-id={{@document.id}}
role="button"
{{on "click" (fn @onClickDocument @document)}}
>
Expand Down
2 changes: 2 additions & 0 deletions addon/components/tag-filter.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
if (includes tag.id this.selectedTagsArray) "uk-background-secondary"
}}"
{{on "click" (fn this.toggleTag tag)}}
data-test-tag
data-test-tag-id={{tag.id}}
>
{{tag.name}}
</button>
Expand Down
18 changes: 0 additions & 18 deletions addon/routes/application.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getOwner } from "@ember/application";
import Route from "@ember/routing/route";
import { inject as service } from "@ember/service";

Expand All @@ -15,27 +14,10 @@ export default class ApplicationRoute extends Route {

@service config;

get controllerInstance() {
const applicationInstance = getOwner(this);
return applicationInstance.lookup("controller:application");
}

model() {}

afterModel(model, transition) {
this.config.alexandriaQueryParams = transition.to.parent.params;
this.config.activeGroup = transition.to.queryParams.activeGroup;

/* If we change the category we need to reset the tags
otherwise the user might end up with no documents and
no ability to reset the filter unless they go back to
the previous category and deselect the filter before navigating
to the new category */
if (
transition.to?.queryParams?.category !==
transition.from?.queryParams?.category
) {
this.controllerInstance.resetTagFilter();
}
}
}
71 changes: 71 additions & 0 deletions tests/acceptance/documents-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,75 @@ module("Acceptance | documents", function (hooks) {
.dom("[data-test-document-list-item].document-list-item-selected")
.doesNotExist();
});

test("changing the category clears the tag selection", async function (assert) {
assert.expect(5);

await this.server.createList("document", 3);
const tag = await this.server.create("tag");
const category = await this.server.create("category");

await visit("/");

assert
.dom(`[data-test-tag-id="${tag.id}"]`)
.doesNotHaveClass(
"uk-background-secondary",
"the tag does not have the selected class"
);

await click(`[data-test-tag-id="${tag.id}"]`);

assert
.dom(`[data-test-tag-id="${tag.id}"]`)
.hasClass(
"uk-background-secondary",
"the tag does has the selected class"
);
assert.equal(
currentURL(),
`/?tags=${tag.id}`,
"tag has been selected and is present in the URL"
);

await click(`[data-test-category-id="${category.id}"]`);

assert.equal(
currentURL(),
`/?category=${category.id}`,
"the category has been set and the tags queryParam has been cleared"
);
assert
.dom(`[data-test-tag-id="${tag.id}"]`)
.doesNotHaveClass(
"uk-background-secondary",
"the tag does not have the selected class"
);
});

test("selecting a document does not clear the tag selection", async function (assert) {
assert.expect(2);

const documents = await this.server.createList("document", 2);
const tag = await this.server.create("tag");

await visit("/");
await click(`[data-test-tag-id="${tag.id}"]`);

assert
.dom(`[data-test-tag-id="${tag.id}"]`)
.hasClass(
"uk-background-secondary",
"the tag does has the selected class"
);

await click(`[data-test-document-list-item-id="${documents[0].id}"]`);

assert
.dom(`[data-test-tag-id="${tag.id}"]`)
.hasClass(
"uk-background-secondary",
"the tag still has the selected class"
);
});
});

0 comments on commit 48e8a0b

Please sign in to comment.