Skip to content

Commit

Permalink
feat(deps): add support for ember-data 4
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Jul 28, 2023
1 parent e75dc73 commit 5bbdb64
Show file tree
Hide file tree
Showing 24 changed files with 379 additions and 180 deletions.
4 changes: 1 addition & 3 deletions addon/components/document-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export default class DocumentCardComponent extends Component {
// Compile an array of original file PKs
const originalFilePKs = encodeURIComponent(
this.args.documents
.map((doc) =>
doc.files.toArray().find((file) => file.variant === "original")
)
.map((doc) => doc.files.find((file) => file.variant === "original"))
.map((f) => f.id)
.join(",")
);
Expand Down
4 changes: 2 additions & 2 deletions addon/components/document-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class DocumentViewComponent extends Component {
filter: this.args.filters || {},
sort: this.sort ? `${this.sortDirection}${this.sort}` : "",
});
const selectedDocs = docs.filter((doc) => docIds.includes(doc.id));
const selectedDocs = [...docs].filter((doc) => docIds.includes(doc.id));
selectedDocs.forEach((doc) => this.documents.selectDocument(doc));
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ export default class DocumentViewComponent extends Component {

this.documents.clearDocumentSelection();
for (let i = startIndex; i <= endIndex; i++) {
this.documents.selectDocument(this.fetchedDocuments.toArray()[i]);
this.documents.selectDocument(this.fetchedDocuments[i]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion addon/components/multi-document-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class MultiDocumentDetailsComponent extends Component {
const tags = [];
const nrOfDocs = this.args.selectedDocuments.length;
const allTags = this.args.selectedDocuments
.map((d) => d.tags.toArray()) // all the tags for a document
.map((d) => d.tags) // all the tags for a document
.flat()
.map((t) => t.name); // produces one large array of all tags

Expand Down
2 changes: 1 addition & 1 deletion addon/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export default class CategoryModel extends LocalizedModel {
@attr color;
@attr metainfo;

@hasMany documents;
@hasMany("document", { inverse: "category", async: true }) documents;
}
6 changes: 3 additions & 3 deletions addon/models/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export default class DocumentModel extends LocalizedModel {
@attr modifiedByUser;
@attr modifiedByGroup;

@belongsTo category;
@hasMany tags;
@hasMany files;
@belongsTo("category", { inverse: "documents", async: true }) category;
@hasMany("tag", { inverse: "documents", async: true }) tags;
@hasMany("file", { inverse: "document", async: true }) files;

get thumbnail() {
const thumbnail = this.files.filter(
Expand Down
6 changes: 3 additions & 3 deletions addon/models/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default class FileModel extends Model {
@attr modifiedByUser;
@attr modifiedByGroup;

@belongsTo document;
@belongsTo("document", { inverse: "files", async: true }) document;

@belongsTo("file", { inverse: "renderings" }) original;
@hasMany("file", { inverse: "original" }) renderings;
@belongsTo("file", { inverse: "renderings", async: true }) original;
@hasMany("file", { inverse: "original", async: true }) renderings;
}
2 changes: 1 addition & 1 deletion addon/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export default class TagModel extends LocalizedModel {
@attr modifiedByUser;
@attr modifiedByGroup;

@hasMany documents;
@hasMany("document", { inverse: "tags", async: true }) documents;
}
3 changes: 3 additions & 0 deletions addon/serializers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import JSONAPISerializer from "@ember-data/serializer/json-api";

export default class ApplicationSerializer extends JSONAPISerializer {}
3 changes: 0 additions & 3 deletions addon/serializers/file.js

This file was deleted.

2 changes: 1 addition & 1 deletion addon/services/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class DocumentsService extends Service {
this.router.externalRouter?.currentRoute?.queryParams?.document;
if (documentQueryParam) {
documentQueryParam.split(",").map(async (id) => {
this.selectDocument(await this.store.find("document", id));
this.selectDocument(await this.store.findRecord("document", id));
});
}
}
Expand Down
10 changes: 5 additions & 5 deletions addon/services/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class TagsService extends Service {
let tag = tagInput;
if (typeof tagInput === "string") {
const tagId = dasherize(tagInput.trim());
const existing = this.allTags.findBy("id", tagId);
const existing = this.allTags.find((tag) => tag.id === tagId);
if (existing) {
tag = existing;
} else {
Expand All @@ -64,11 +64,11 @@ export default class TagsService extends Service {
}
}

if (document.tags.findBy("id", tag.id)) {
if ((await document.tags).find((t) => t.id === tag.id)) {
return tag;
}

document.tags.pushObject(tag);
(await document.tags).push(tag);
await document.save();

await this.fetchAllTags.perform();
Expand All @@ -85,10 +85,10 @@ export default class TagsService extends Service {
*/
@action async remove(document, tag) {
if (typeof tag === "string") {
tag = this.allTags.findBy("name", tag);
tag = this.allTags.find((t) => t.name === tag);
}

document.tags.removeObject(tag);
document.tags = (await document.tags).filter((t) => t !== tag);
await document.save();

this.fetchSearchTags.perform();
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"ember-cli-htmlbars": "^6.2.0",
"ember-composable-helpers": "^5.0.0",
"ember-concurrency": "^3.0.0",
"ember-data": "^3.28.13",
"ember-data": "^3.28.13 || ^4.12.3",
"ember-engines-router-service": "^0.3.0",
"ember-fetch": "^8.1.2",
"ember-intl": "^5.7.2",
Expand Down Expand Up @@ -108,9 +108,6 @@
"stylelint-scss": "5.0.1",
"webpack": "5.88.2"
},
"resolutions": {
"ember-data": "^3.28.13"
},
"ember": {
"edition": "octane"
},
Expand Down
3 changes: 1 addition & 2 deletions tests/acceptance/documents-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "@ember/test-helpers";
import { setupApplicationTest } from "dummy/tests/helpers";
import { setupMirage } from "ember-cli-mirage/test-support";
import { setupIntl, setLocale } from "ember-intl/test-support";
import { setLocale } from "ember-intl/test-support";
import { module, test } from "qunit";

import setupRequestAssertions from "../helpers/assert-request";
Expand All @@ -17,7 +17,6 @@ module("Acceptance | documents", function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
setupRequestAssertions(hooks);
setupIntl(hooks, ["en"]);

test("document grid displays documents", async function (assert) {
const documents = this.server.createList("document", 5);
Expand Down
23 changes: 6 additions & 17 deletions tests/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import makeServer from "dummy/mirage/config";
import engineResolverFor from "ember-engines/test-support/engine-resolver-for";
import { setupIntl } from "ember-intl/test-support";
import {
setupApplicationTest as upstreamSetupApplicationTest,
setupRenderingTest as upstreamSetupRenderingTest,
Expand Down Expand Up @@ -42,32 +43,20 @@ function setupEnginesRouter(hooks) {

function setupApplicationTest(hooks, options) {
upstreamSetupApplicationTest(hooks, options);

// Additional setup for application tests can be done here.
//
// For example, if you need an authenticated session for each
// application test, you could do:
//
// hooks.beforeEach(async function () {
// await authenticateSession(); // ember-simple-auth
// });
//
// This is also a good place to call test setup functions coming
// from other addons:
//
// setupIntl(hooks); // ember-intl
// setupMirage(hooks); // ember-cli-mirage
setupIntl(hooks, "en");
}

function setupRenderingTest(hooks, options = {}) {
upstreamSetupRenderingTest(hooks, { ...options, resolver });
upstreamSetupRenderingTest(hooks, { resolver, ...options });
setupMirageServer(hooks);
setupIntl(hooks, "en");
setupEnginesRouter(hooks);
}

function setupTest(hooks, options = {}) {
upstreamSetupTest(hooks, { ...options, resolver });
upstreamSetupTest(hooks, { resolver, ...options });
setupMirageServer(hooks);
setupIntl(hooks, "en");
setupEnginesRouter(hooks);
}

Expand Down
2 changes: 0 additions & 2 deletions tests/integration/components/category-nav-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { render } from "@ember/test-helpers";
import { setupRenderingTest } from "dummy/tests/helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupMirage } from "ember-cli-mirage/test-support";
import { setupIntl } from "ember-intl/test-support";
import { module, test } from "qunit";

module("Integration | Component | category-nav", function (hooks) {
setupRenderingTest(hooks);
setupIntl(hooks, "en");
setupMirage(hooks);

test("it renders category nav", async function (assert) {
Expand Down
2 changes: 0 additions & 2 deletions tests/integration/components/category-nav/category-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { render } from "@ember/test-helpers";
import { setupRenderingTest } from "dummy/tests/helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupIntl } from "ember-intl/test-support";
import { module, test } from "qunit";

module("Integration | Component | category-nav/category", function (hooks) {
setupRenderingTest(hooks);
setupIntl(hooks, "en");

test("it renders a category", async function (assert) {
this.category = { name: "test", color: "#f00", id: 1 };
Expand Down
2 changes: 0 additions & 2 deletions tests/integration/components/document-delete-button-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { render, click } from "@ember/test-helpers";
import { setupRenderingTest } from "dummy/tests/helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupIntl } from "ember-intl/test-support";
import { module, test } from "qunit";

module("Integration | Component | document-delete-button", function (hooks) {
setupRenderingTest(hooks);
setupIntl(hooks, "en");

test("delete document", async function (assert) {
this.document = {
Expand Down
2 changes: 0 additions & 2 deletions tests/integration/components/document-list-item-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { click, render } from "@ember/test-helpers";
import { setupRenderingTest } from "dummy/tests/helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupMirage } from "ember-cli-mirage/test-support";
import { setupIntl } from "ember-intl/test-support";
import { module, test } from "qunit";

module("Integration | Component | document-list-item", function (hooks) {
setupRenderingTest(hooks);
setupIntl(hooks, "en");
setupMirage(hooks);

hooks.beforeEach(async function () {
Expand Down
2 changes: 0 additions & 2 deletions tests/integration/components/empty-icon-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { render } from "@ember/test-helpers";
import { setupRenderingTest } from "dummy/tests/helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupIntl } from "ember-intl/test-support";
import { module, test } from "qunit";

module("Integration | Component | empty-icon", function (hooks) {
setupRenderingTest(hooks);
setupIntl(hooks, "en");

test("it renders", async function (assert) {
await render(hbs`<EmptyIcon />`);
Expand Down
2 changes: 0 additions & 2 deletions tests/integration/components/single-document-details-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { tracked } from "@glimmer/tracking";
import { setupRenderingTest } from "dummy/tests/helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupMirage } from "ember-cli-mirage/test-support";
import { setupIntl } from "ember-intl/test-support";
import { module, test } from "qunit";
import sinon from "sinon";

Expand All @@ -18,7 +17,6 @@ const mockDocumentsService = class DocumentsService extends Service {

module("Integration | Component | single-document-details", function (hooks) {
setupRenderingTest(hooks);
setupIntl(hooks, "en");
setupMirage(hooks);

hooks.beforeEach(function () {
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/serializers/application-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { setupTest } from "dummy/tests/helpers";
import { module, test } from "qunit";

module("Unit | Serializer | application", function (hooks) {
setupTest(hooks);

test("it exists", function (assert) {
const store = this.owner.lookup("service:store");
const serializer = store.serializerFor("application");

assert.ok(serializer);
});
});
24 changes: 18 additions & 6 deletions tests/unit/services/documents-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ module("Unit | Service | documents", function (hooks) {
});

test("it uploads documents", async function (assert) {
const requests = this.server.pretender.handledRequests;

const service = this.owner.lookup("service:documents");
const store = this.owner.lookup("service:store");

const category = this.server.create("category");
const category = await store.findRecord(
"category",
this.server.create("category").id
);
const files = [
new File(["1"], "test1.docx"),
new File(["2"], "test2.docx"),
Expand All @@ -25,6 +27,10 @@ module("Unit | Service | documents", function (hooks) {

await service.upload(category, files);

const requests = this.server.pretender.handledRequests.filter(
(request) => !request.url.includes("categories")
);

// Each file generates three requests.
assert.strictEqual(requests.length, files.length * 3);

Expand All @@ -45,15 +51,21 @@ module("Unit | Service | documents", function (hooks) {
});

test("it replaces documents", async function (assert) {
const requests = this.server.pretender.handledRequests;

const service = this.owner.lookup("service:documents");
const store = this.owner.lookup("service:store");

const document = this.server.create("document");
const document = await store.findRecord(
"document",
this.server.create("document").id
);
const file = new File([""], "test.docx");

await service.replace(document, file);

const requests = this.server.pretender.handledRequests.filter(
(request) => !request.url.includes("documents")
);

assert.strictEqual(requests.length, 2);
assert.ok(requests[0].url.endsWith("files"));
assert.ok(requests[1].url.endsWith("file-upload"));
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/services/tags-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module("Unit | Service | tags", function (hooks) {
]
);

assert.ok(document.tags.includes(tag));
assert.ok((await document.tags).includes(tag));
});

test("it adds new tags", async function (assert) {
Expand Down Expand Up @@ -64,7 +64,7 @@ module("Unit | Service | tags", function (hooks) {
]
);

assert.ok(document.tags.findBy("name", tag));
assert.ok((await document.tags).find((t) => t.name === tag));
});

test("it removes tags", async function (assert) {
Expand All @@ -76,7 +76,7 @@ module("Unit | Service | tags", function (hooks) {
await service.fetchAllTags.perform();

const document = await store.createRecord("document").save();
const tag = document.tags.firstObject;
const tag = (await document.tags)[0];

await service.remove(document, tag);

Expand All @@ -90,6 +90,6 @@ module("Unit | Service | tags", function (hooks) {
]
);

assert.notOk(document.tags.includes(tag));
assert.notOk((await document.tags).includes(tag));
});
});
Loading

0 comments on commit 5bbdb64

Please sign in to comment.