From 6e17511f297dc00a187deed978d775eb5d0f9a44 Mon Sep 17 00:00:00 2001
From: Eugen Ciur <eugen@papermerge.com>
Date: Mon, 11 Oct 2021 07:15:55 +0200
Subject: [PATCH] very first unit test

---
 app/utils/index.js       |  4 +-
 tests/unit/utils-test.js | 93 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 1 deletion(-)
 create mode 100644 tests/unit/utils-test.js

diff --git a/app/utils/index.js b/app/utils/index.js
index 6df24f1..a5f7525 100644
--- a/app/utils/index.js
+++ b/app/utils/index.js
@@ -37,7 +37,9 @@ function group_perms_by_model(permissions) {
   groups = new Set(groups);
 
   groups.forEach(model => {
-    let perms = permissions.filter(item => item.content_type.get('model') === model);
+    let perms = permissions.filter(
+      item => item.content_type.get('model') === model
+    );
     result.push({model, perms}); // same as result.push({mode: model, perms: perms})
   });
 
diff --git a/tests/unit/utils-test.js b/tests/unit/utils-test.js
new file mode 100644
index 0000000..d7eda0d
--- /dev/null
+++ b/tests/unit/utils-test.js
@@ -0,0 +1,93 @@
+import { module, test } from 'qunit';
+import group_perms_by_model from 'papermerge/utils';
+
+class FakeContentType {
+  constructor(name) {
+    this.name = name;
+  }
+
+  get(whatever) {
+    return this.name;
+  }
+}
+
+
+module('Unit | Utility', function() {
+  test('group_perms_by_model works for basic case', function(assert) {
+      let grouped_permissions,
+        permissions,
+        expected_result,
+        models,
+        expected_models,
+        areSetsEqual;
+
+      permissions = [
+        {
+          attr_1x: 'attr_1x',
+          attr_2x: 'attr_2x',
+          content_type: new FakeContentType('m1')
+        },
+        {
+          attr_1y: 'attr_1y',
+          attr_2y: 'attr_2y',
+          content_type: new FakeContentType('m1')
+        },
+        {
+          attr_1z: 'attr_1z',
+          attr_2z: 'attr_2z',
+          content_type: new FakeContentType('m1')
+        },
+        {
+          attr_1w: 'attr_1w',
+          attr_2w: 'attr_2w',
+          content_type: new FakeContentType('m2')
+        },
+      ];
+
+      expected_result = [
+        {
+          model: new FakeContentType('m1'),
+          perms: [
+            {
+              attr_1x: 'attr_1x',
+              attr_2x: 'attr_2x',
+              content_type: new FakeContentType('m1')
+            },
+            {
+              attr_1y: 'attr_1y',
+              attr_2y: 'attr_2y',
+              content_type: new FakeContentType('m1')
+            },
+            {
+              attr_1z: 'attr_1z',
+              attr_2z: 'attr_2z',
+              content_type: new FakeContentType('m1')
+            },
+          ]
+        },
+        {
+          model: new FakeContentType('m2'),
+          perms: [
+            {
+              attr_1w: 'attr_1w',
+              attr_2w: 'attr_2w',
+              content_type: new FakeContentType('m2')
+            },
+          ]
+        }
+      ];
+
+      grouped_permissions = group_perms_by_model(permissions);
+
+      assert.strictEqual(
+        grouped_permissions.length, expected_result.length
+      );
+
+      models = new Set(grouped_permissions.map(item => item.model));
+      expected_models = new Set(['m1', 'm2']);
+
+      areSetsEqual = (a, b) => a.size === b.size && [...a].every(value => b.has(value));
+      assert.true(areSetsEqual(models, expected_models));
+
+  });
+});
\ No newline at end of file
-- 
GitLab