From c4edc9122d8ac2be89b5dae5432046f0481575bd Mon Sep 17 00:00:00 2001
From: Eugen Ciur <eugen@papermerge.com>
Date: Mon, 11 Oct 2021 07:33:08 +0200
Subject: [PATCH] very basic unit tests for utility functions

---
 app/components/permissions/index.js |  2 +-
 app/utils/index.js                  | 14 ++++++++++++-
 tests/unit/utils-test.js            | 31 +++++++++++++++++++++++++----
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/app/components/permissions/index.js b/app/components/permissions/index.js
index c164de3..fae46f5 100644
--- a/app/components/permissions/index.js
+++ b/app/components/permissions/index.js
@@ -1,5 +1,5 @@
 import Component from '@glimmer/component';
-import group_perms_by_model from 'papermerge/utils';
+import { group_perms_by_model } from 'papermerge/utils';
 
 
 
diff --git a/app/utils/index.js b/app/utils/index.js
index a5f7525..7f821a0 100644
--- a/app/utils/index.js
+++ b/app/utils/index.js
@@ -46,4 +46,16 @@ function group_perms_by_model(permissions) {
   return result;
 }
 
-export default group_perms_by_model;
\ No newline at end of file
+function are_sets_equal(set1, set2) {
+  let same_size, same_values;
+
+  same_size = (a, b) => a.size === b.size;
+  same_values = (a, b) => [...a].every(value => b.has(value));
+
+  return same_size(set1, set2) && same_values(set1, set2);
+}
+
+export {
+  group_perms_by_model,
+  are_sets_equal
+};
\ No newline at end of file
diff --git a/tests/unit/utils-test.js b/tests/unit/utils-test.js
index d7eda0d..b52ab04 100644
--- a/tests/unit/utils-test.js
+++ b/tests/unit/utils-test.js
@@ -1,5 +1,8 @@
 import { module, test } from 'qunit';
-import group_perms_by_model from 'papermerge/utils';
+import {
+  are_sets_equal,
+  group_perms_by_model
+} from 'papermerge/utils';
 
 class FakeContentType {
   constructor(name) {
@@ -13,7 +16,28 @@ class FakeContentType {
 
 
 module('Unit | Utility', function() {
-  test('group_perms_by_model works for basic case', function(assert) {
+
+  test('are_sets_qual', function(assert) {
+    let set1, set2;
+
+    set1 = new Set([8, 1]);
+    set2 = new Set([1, 8]);
+
+    assert.true(
+      are_sets_equal(set1, set2),
+      'set[1, 8] expected to be eq to set[8, 1]'
+    );
+
+    set1 = new Set([8, 1]);
+    set2 = new Set([1, 8, 2]);
+
+    assert.false(
+      are_sets_equal(set1, set2),
+      'set[1, 8, 2] expected NOT to be eq to set[8, 1]'
+    );
+  });
+
+  test('group_perms_by_model', function(assert) {
       let grouped_permissions,
         permissions,
         expected_result,
@@ -86,8 +110,7 @@ module('Unit | Utility', function() {
       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));
+      assert.true(are_sets_equal(models, expected_models));
 
   });
 });
\ No newline at end of file
-- 
GitLab