import { unlinkObservable, createSymlinkObservable } from "./..";

import { toArray } from "rxjs/operators";

jest.mock("fs");
const fs = require("fs");

describe("unlinkObservable", () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });
  it("errors on unlink issue", () => {
    expect.assertions(3);
    fs.existsSync.mockImplementation(() => true);
    fs.unlink.mockImplementation((path, error) => error({ message: "lol" }));
    unlinkObservable("path").subscribe(
      () => {},
      err => {
        expect(err.message).toBe("lol");
      },
      () => done.fail()
    );
    expect(fs.existsSync).toBeCalledWith("path");
    expect(fs.unlink).toBeCalled();
  });
  it("completes and calls fs.existsSync, fs.unlink", () => {
    expect.assertions(2);
    fs.existsSync.mockImplementation(() => true);
    fs.unlink.mockImplementation(() => true);
    unlinkObservable("path2")
      .pipe(toArray())
      .subscribe(() => {}, err => done.fail(err), () => {});
    expect(fs.existsSync).toBeCalledWith("path2");
    expect(fs.unlink).toBeCalled();
  });
});

@@ -264,29 +264,44 @@
 ink issue%22, 
-() =%3E
+async function(done)
  %7B%0A    expec
@@ -317,16 +317,58 @@
 ons(3);%0A
+    // File exists so we try to unlink it%0A
     fs.e
@@ -417,155 +417,298 @@
     
-fs.unlink.mockImplementation((path, error) =%3E error(%7B message: %22lol%22 %7D));%0A    unlinkObservable(%22path%22).subscribe(%0A      () =%3E %7B%7D,%0A      err =%3E
+// Unlink fails for some reason (permissions, etc.)%0A    fs.unlink.mockImplementation((path, callback) =%3E%0A      callback(%7B message: %22forced failure%22 %7D)%0A    );%0A    try %7B%0A      await unlinkObservable(%22path%22).toPromise();%0A      // Should not pass through%0A      done.fail();%0A    %7D catch (err)
  %7B%0A
-  
     
@@ -739,54 +739,31 @@
 Be(%22
-lol%22);%0A      %7D,%0A      () =%3E done.fail()
+forced failure%22);
 %0A    
-);
+%7D
 %0A   
@@ -841,24 +841,36 @@
 BeCalled();%0A
+    done();%0A
   %7D);%0A  it(%22
@@ -916,21 +916,32 @@
 nlink%22, 
-() =%3E
+async function()
  %7B%0A    e
@@ -1045,32 +1045,59 @@
 tation((
-) =%3E true
+path, callback) =%3E callback()
 );%0A
+%0A
     
+await 
 unlinkOb
@@ -1117,92 +1117,22 @@
 h2%22)
-%0A      .pipe(toArray())%0A      .subscribe(() =%3E %7B%7D, err =%3E done.fail(err), () =%3E %7B%7D);
+.toPromise();%0A
 %0A   

improve style of old fs-observable tests