Hello! I am really struggling for a few days alrea...
# typescript
n
Hello! I am really struggling for a few days already. I hope, someone can help me somehow or at least, point on what I am doing wrong. So, I am trying to apply some Approval Rules to GitLab (but that does not really matter). My struggle is unit testing. There are a lot of examples on testing methods that have any return value. My method just searches for the group, for the project inside the group and creates resources (Approval Rules). I do not have an idea how to test this method. My thought was to check whether the newResource callback was called and what resources did it create The code of this method is:
Copy code
export async function applyRules(group: string, rules: Approval[]) {
  let groupId = await findGroupsIds([group]);
  if (groupId.length == 0) return;

  try {
    const groupProjects = await gitlab.getProjects({
      groupId: groupId[0],
      orderBy: "name",
      includeSubgroups: true,
      withShared: false,
    });

    groupProjects.projects.forEach(async (project) => {
      const branches = await gitlab.getProjectProtectedBranches({
        projectId: project.pathWithNamespace,
      });
      let branchIds = branches.protectedBranches.map(branch => branch.id);
      const repoName = `${project.path.toLowerCase()}-${project.id}`;
      rules.forEach((rule) => {
        const ruleName = rule.name.replace(/\s/g, '-').toLowerCase();
        const projectApproval = new gitlab.ProjectApprovalRule(`${repoName}-${ruleName}`, {
          approvalsRequired: 1,
          project: project.id.toString(),
          name: rule.name,
          userIds: rule.userIds,
          groupIds: rule.groupIds,
          protectedBranchIds: branchIds,
        });
      });
    });
  } catch (err) {
    console.log(`Error: ${String(err).split('*').pop()}`)
  }
}
Sure, the code is not perfect in some ways