zipfile.tests.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var assert = require("assert");
  2. var JSZip = require("jszip");
  3. var zipfile = require("../lib/zipfile");
  4. var test = require("./test")(module);
  5. test('file in zip can be read after being written', function() {
  6. return emptyZipFile().then(function(zip) {
  7. assert(!zip.exists("song/title"));
  8. zip.write("song/title", "Dark Blue");
  9. assert(zip.exists("song/title"));
  10. return zip.read("song/title", "utf8").then(function(contents) {
  11. assert.equal(contents, "Dark Blue");
  12. });
  13. });
  14. });
  15. function emptyZipFile() {
  16. var zip = new JSZip();
  17. return zip.generateAsync({type: "arraybuffer"}).then(function(arrayBuffer) {
  18. return zipfile.openArrayBuffer(arrayBuffer);
  19. });
  20. }
  21. test("splitPath splits zip paths on last forward slash", function() {
  22. assert.deepEqual(zipfile.splitPath("a/b"), {dirname: "a", basename: "b"});
  23. assert.deepEqual(zipfile.splitPath("a/b/c"), {dirname: "a/b", basename: "c"});
  24. assert.deepEqual(zipfile.splitPath("/a/b/c"), {dirname: "/a/b", basename: "c"});
  25. });
  26. test("when path has no forward slashes then splitPath returns empty dirname", function() {
  27. assert.deepEqual(zipfile.splitPath("name"), {dirname: "", basename: "name"});
  28. });
  29. test("joinPath joins arguments with forward slashes", function() {
  30. assert.equal(zipfile.joinPath("a", "b"), "a/b");
  31. assert.equal(zipfile.joinPath("a/b", "c"), "a/b/c");
  32. assert.equal(zipfile.joinPath("a", "b/c"), "a/b/c");
  33. assert.equal(zipfile.joinPath("/a/b", "c"), "/a/b/c");
  34. });
  35. test("empty parts are ignored when joining paths", function() {
  36. assert.equal(zipfile.joinPath("a", ""), "a");
  37. assert.equal(zipfile.joinPath("", "b"), "b");
  38. assert.equal(zipfile.joinPath("a", "", "b"), "a/b");
  39. });
  40. test("when joining paths then absolute paths ignore earlier paths", function() {
  41. assert.equal(zipfile.joinPath("a", "/b"), "/b");
  42. assert.equal(zipfile.joinPath("a", "/b", "c"), "/b/c");
  43. assert.equal(zipfile.joinPath("/a", "/b"), "/b");
  44. assert.equal(zipfile.joinPath("/a"), "/a");
  45. });